protected function addItem(MenuItem $root, $config) { foreach ($config['roles'] as $role) { if (!$this->security->isGranted($role)) { return null; } } $name = $config['label']; $label = isset($config['icon']) ? $config['icon'] . ' ' : ' '; $label .= "<span class='sidebar-title'>" . $config['label'] . "</span>"; $label .= isset($config['items']) && count($config['items']) ? '<span class="caret"></span>' : ''; $options = array('label' => $label, 'extras' => array('safe_label' => true)); if ('#' == $config['route']) { $options['uri'] = '#'; } elseif ($config['route']) { $options['route'] = $config['route']; } if ($root->getChild($name)) { $name .= microtime(); } $MenuItem = $root->addChild($name, $options); if (isset($config['class'])) { $MenuItem->setAttribute('class', $config['class']); } if (isset($config['items'])) { foreach ($config['items'] as $item) { $this->addItem($MenuItem, $item); } if ($root->getLevel()) { $root->setChildrenAttribute('class', 'nav sub-nav'); $root->setLinkAttribute('class', 'accordion-toggle'); } } }
/** * Customize menu * * @param MenuItem $menu * * @return MenuItem */ protected function customizeMenu(MenuItem $menu) { $menu->setChildrenAttribute('class', 'sidebar_content'); foreach ($menu->getChildren() as $child) { $child->setAttribute('class', 'sidebar_item ' . $child->getAttribute('class')); $child->setLinkAttribute('class', 'sidebar_link'); if ($child->hasChildren()) { foreach ($child->getChildren() as $subChild) { $subChild->setAttribute('class', 'sidebar_submenu_item ' . $subChild->getAttribute('class')); $subChild->setLinkAttribute('class', 'sidebar_submenu_link'); } /*$child->setAttribute('class', 'toggle open '. $child->getAttribute('class'));*/ $child->setChildrenAttribute('class', 'sidebar_submenu'); $child->setExtra('safe_label', true); $child->setLabel($child->getLabel() . ' <span class="arrow"></span>'); } } return $menu; }
public function testRenderSubMenuPortionWithClassAndTitle() { $this->pt2->setChildrenAttribute('class', 'parent2_class')->setChildrenAttribute('title', 'parent2 title'); $rendered = '<ul class="parent2_class" title="parent2 title"><li class="first last"><span>Child 4</span><ul class="menu_level_2"><li class="first last"><span>Grandchild 1</span></li></ul></li></ul>'; $this->assertEquals($rendered, $this->renderer->render($this->menu['Parent 2'])); }
/** * Customize menu * * @param MenuItem $menu * * @return MenuItem */ protected function customizeMenu(MenuItem $menu) { $menu->setChildrenAttribute('class', 'sidebar_content'); $request = $this->get('core.request.current'); foreach ($menu->getChildren() as $child) { $child->setAttribute('class', 'sidebar_item ' . $child->getAttribute('class')); $uri = $request->getUri(); $itemUri = $child->getUri(); $linkClass = $uri == $itemUri || stripos($uri, $itemUri) !== false ? ' active' : ''; $child->setLinkAttribute('class', 'sidebar_link' . $linkClass); if ($child->hasChildren()) { foreach ($child->getChildren() as $subChild) { $subChild->setAttribute('class', 'sidebar_submenu_item ' . $subChild->getAttribute('class')); $subLinkClass = $request->getUri() == $subChild->getUri() ? ' active' : ''; $subChild->setLinkAttribute('class', 'sidebar_submenu_link' . $subLinkClass); } $openClass = stripos($uri, $itemUri) === false ? '' : 'active'; $child->setChildrenAttribute('class', 'sidebar_submenu ' . $openClass); $child->setExtra('safe_label', true); $child->setLabel($child->getLabel() . ' <span class="arrow"></span>'); } } return $menu; }