Back to AbstractMenu class

Method getItems

public \Joomla\CMS\Menu\MenuItem|\Joomla\CMS\Menu\MenuItem[]
getItems
(mixed $attributes, mixed $values, mixed $firstonly = false)
Gets menu items by attribute
Parameters
  • mixed $attributes The field name(s).
  • mixed $values The value(s) of the field. If an array, need to match field names each attribute may have multiple values to lookup for.
  • bool $firstonly If true, only returns the first item found
Returns
  • \Joomla\CMS\Menu\MenuItem|\Joomla\CMS\Menu\MenuItem[] An array of menu item objects or a single object if the $firstonly parameter is true
Since
  • 1.5
Class: AbstractMenu
Project: Joomla

Method getItems - Source code

/**
 * Gets menu items by attribute
 *
 * @param   mixed    $attributes  The field name(s).
 * @param   mixed    $values      The value(s) of the field. If an array, need to match field names
 *                                each attribute may have multiple values to lookup for.
 * @param   boolean  $firstonly   If true, only returns the first item found
 *
 * @return  MenuItem|MenuItem[]  An array of menu item objects or a single object if the $firstonly parameter is true
 *
 * @since   1.5
 */
public function getItems($attributes, $values, $firstonly = false)
{
    $items = array();
    $attributes = (array) $attributes;
    $values = (array) $values;
    $count = \count($attributes);
    foreach ($this->getMenu() as $item) {
        if (!\is_object($item)) {
            continue;
        }
        $test = true;
        for ($i = 0; $i < $count; $i++) {
            if (\is_array($values[$i])) {
                if (!\in_array($item->{$attributes[$i]}, $values[$i])) {
                    $test = false;
                    break;
                }
            } else {
                if ($item->{$attributes[$i]} != $values[$i]) {
                    $test = false;
                    break;
                }
            }
        }
        if ($test) {
            if ($firstonly) {
                return $item;
            }
            $items[] = $item;
        }
    }
    return $items;
}