Back to MenuitemField class

Method getGroups

protected array
getGroups
()
Method to get the field option groups.
Returns
  • array The field option objects as a nested array in groups.
Since
  • 1.6
Class: MenuitemField
Project: Joomla

Method getGroups - Source code

/**
 * Method to get the field option groups.
 *
 * @return  array  The field option objects as a nested array in groups.
 *
 * @since   1.6
 */
protected function getGroups()
{
    $groups = array();
    $menuType = $this->menuType;
    // Get the menu items.
    $items = MenusHelper::getMenuLinks($menuType, 0, 0, $this->published, $this->language, $this->clientId);
    // Build group for a specific menu type.
    if ($menuType) {
        // If the menutype is empty, group the items by menutype.
        $db = Factory::getDbo();
        $query = $db->getQuery(true)->select($db->quoteName('title'))->from($db->quoteName('#__menu_types'))->where($db->quoteName('menutype') . ' = :menuType')->bind(':menuType', $menuType);
        $db->setQuery($query);
        try {
            $menuTitle = $db->loadResult();
        } catch (\RuntimeException $e) {
            $menuTitle = $menuType;
        }
        // Initialize the group.
        $groups[$menuTitle] = array();
        // Build the options array.
        foreach ($items as $link) {
            $levelPrefix = str_repeat('- ', max(0, $link->level - 1));
            // Displays language code if not set to All
            if ($link->language !== '*') {
                $lang = ' (' . $link->language . ')';
            } else {
                $lang = '';
            }
            $groups[$menuTitle][] = HTMLHelper::_('select.option', $link->value, $levelPrefix . $link->text . $lang, 'value', 'text', \in_array($link->type, $this->disable));
        }
    } else {
        // Build the groups arrays.
        foreach ($items as $menu) {
            // Initialize the group.
            $groups[$menu->title] = array();
            // Build the options array.
            foreach ($menu->links as $link) {
                $levelPrefix = str_repeat('- ', max(0, $link->level - 1));
                // Displays language code if not set to All
                if ($link->language !== '*') {
                    $lang = ' (' . $link->language . ')';
                } else {
                    $lang = '';
                }
                $groups[$menu->title][] = HTMLHelper::_('select.option', $link->value, $levelPrefix . $link->text . $lang, 'value', 'text', \in_array($link->type, $this->disable));
            }
        }
    }
    // Merge any additional groups in the XML definition.
    $groups = array_merge(parent::getGroups(), $groups);
    return $groups;
}