TbHtml::menu PHP Method

menu() public static method

Generates a menu.
public static menu ( array $items, array $htmlOptions = [], integer $depth ) : string
$items array the menu items.
$htmlOptions array additional HTML attributes.
$depth integer the current depth.
return string the generated menu.
    public static function menu(array $items, $htmlOptions = array(), $depth = 0)
    {
        // todo: consider making this method protected.
        if (!empty($items)) {
            $htmlOptions['role'] = 'menu';
            $output = self::openTag('ul', $htmlOptions);
            foreach ($items as $itemOptions) {
                if (is_string($itemOptions)) {
                    if ($itemOptions == '---') {
                        $output .= self::menuDivider();
                    } else {
                        $output .= $itemOptions;
                    }
                } else {
                    if (TbArray::popValue('visible', $itemOptions, true) === false) {
                        continue;
                    }
                    // todo: consider removing the support for htmlOptions.
                    $options = TbArray::popValue('htmlOptions', $itemOptions, array());
                    if (!empty($options)) {
                        $itemOptions = TbArray::merge($options, $itemOptions);
                    }
                    $label = TbArray::popValue('label', $itemOptions, '');
                    if (TbArray::popValue('active', $itemOptions, false)) {
                        self::addCssClass('active', $itemOptions);
                    }
                    if (TbArray::popValue('disabled', $itemOptions, false)) {
                        self::addCssClass('disabled', $itemOptions);
                    }
                    if (!isset($itemOptions['linkOptions'])) {
                        $itemOptions['linkOptions'] = array();
                    }
                    $icon = TbArray::popValue('icon', $itemOptions);
                    if (!empty($icon)) {
                        $label = self::icon($icon) . ' ' . $label;
                    }
                    $items = TbArray::popValue('items', $itemOptions, array());
                    $url = TbArray::popValue('url', $itemOptions, false);
                    if (empty($items)) {
                        if (!$url) {
                            $output .= self::menuHeader($label);
                        } else {
                            $itemOptions['linkOptions']['tabindex'] = -1;
                            $output .= self::menuLink($label, $url, $itemOptions);
                        }
                    } else {
                        $output .= self::menuDropdown($label, $url, $items, $itemOptions, $depth);
                    }
                }
            }
            $output .= '</ul>';
            return $output;
        } else {
            return '';
        }
    }

Usage Example

Example #1
0
 public function testMenu()
 {
     $I = $this->codeGuy;
     $items = array(array('icon' => TbHtml::ICON_HOME, 'label' => 'Home', 'url' => '#'), array('label' => 'Profile', 'url' => '#', 'htmlOptions' => array('disabled' => true)), array('label' => 'Dropdown', 'active' => true, 'items' => array(array('label' => 'Action', 'url' => '#'), array('label' => 'Another action', 'url' => '#'), array('label' => 'Dropdown', 'items' => array(array('label' => 'Action', 'url' => '#'))), TbHtml::menuDivider(), array('label' => 'Separate link', 'url' => '#'))), array('label' => 'Hidden', 'url' => '#', 'visible' => false));
     $html = TbHtml::menu($items, array('class' => 'ul'));
     $nav = $I->createNode($html, 'ul');
     $I->seeNodeAttribute($nav, 'role', 'menu');
     $I->seeNodeNumChildren($nav, 3);
     foreach ($nav->children() as $i => $liElement) {
         $li = $I->createNode($liElement);
         if ($i === 2) {
             $I->seeNodeCssClass($li, 'dropdown active');
             $I->seeNodeChildren($li, array('a.dropdown-toggle', 'ul.dropdown-menu'));
             $ul = $li->filter('ul.dropdown-menu');
             $I->seeNodeNumChildren($ul, 5);
             foreach ($ul->children() as $j => $subLiElement) {
                 $subLi = $I->createNode($subLiElement);
                 if ($j === 2) {
                     $I->seeNodeCssClass($subLi, 'dropdown-submenu');
                     $I->seeNodeChildren($subLi, array('a.dropdown-toggle', 'ul.dropdown-menu'));
                     $subUl = $subLi->filter('ul.dropdown-menu');
                     $I->seeNodeNumChildren($subUl, 1);
                 } else {
                     if ($j === 3) {
                         $I->seeNodeCssClass($subLi, 'divider');
                     } else {
                         $subA = $subLi->filter('a');
                         $I->seeNodeText($subA, $items[$i]['items'][$j]['label']);
                     }
                 }
             }
         } else {
             if ($i === 0) {
                 $I->seeNodeChildren($li, array('i.icon-home', 'a'));
             }
             if ($i === 2) {
                 $I->seeNodeCssClass($li, 'disabled');
             }
             $a = $li->filter('a');
             $I->seeNodeAttributes($a, array('href' => '#', 'tabindex' => '-1'));
             $I->seeNodeText($a, $items[$i]['label']);
         }
     }
     $html = TbHtml::menu(array());
     $this->assertEquals('', $html);
 }
TbHtml