public function testMoveToPosition() { $menu = new MenuItem('root', new MenuFactory()); $menu->addChild('c1'); $menu->addChild('c2'); $menu->addChild('c3'); $menu->addChild('c4'); $manipulator = new MenuManipulator(); $manipulator->moveToPosition($menu['c1'], 2); $this->assertEquals(array('c2', 'c3', 'c1', 'c4'), array_keys($menu->getChildren())); }
/** * Add a child to the menu * * @param KnpMenu $menu * @param Menu $item */ protected function addChild(KnpMenu $menu, Menu $item) { $name = $item->getName() . $item->getId(); $options = array(); $options['label'] = ucfirst($item->getName()); $options['extras'] = array('safe_label' => true); if (!$item instanceof MenuGroup) { if ($uri = $item->getLink()) { $options['uri'] = $uri; } elseif ($content = $item->getContent()) { $options['route'] = '_content'; $slug = $content->getSlug(); if (substr($slug, -6) == '/index') { $slug = rtrim($slug, "index"); } $options['routeParameters'] = ['slug' => $slug]; } } else { $options['uri'] = ''; } $menu->addChild($name, $options); if ($item->hasChildren()) { $menu[$name]->setChildrenAttribute('class', 'sub-nav'); foreach ($item->getChildren() as $child) { $this->addChild($menu[$name], $child); } } }
public function testRenderOrderedList() { $menu = new MenuItem('test', new MenuFactory()); $menu->addChild('About')->addChild('foobar'); $rendered = '<ol><li class="first last"><span>About</span><ol class="menu_level_1"><li class="first last"><span>foobar</span></li></ol></li></ol>'; $this->assertEquals($rendered, $this->createRenderer()->render($menu, array('template' => 'knp_menu_ordered.html.twig'))); }
public function addMenuParts(MenuItem $menu) { $user = $this->securityContext->getToken()->getUser(); if (is_object($user)) { $menu->addChild('Profile', array('route' => 'fos_user_profile_show', 'routeParameters' => array('id' => $user->getId()))); } }
protected function addIdNameMenu(MenuItem $menu) { $menu->addChild('IdName', array('uri' => '#', 'label' => '<i class="fa fa-bed"></i> IdName<i class="fa fa-angle-double-left pull-right"></i></a>', 'extras' => array('safe_label' => true), 'attributes' => array('class' => 'treeview'))); $menu['IdName']->setChildrenAttribute('class', 'treeview-menu'); $menu['IdName']->addChild('AddIdName', array('label' => 'Tambah IdName', 'route' => 'app_idname_new', 'attributes' => array('class' => 'treeview'))); $menu['IdName']->addChild('ListIdName', array('label' => 'Daftar IdName', 'route' => 'app_idname_list', 'attributes' => array('class' => 'treeview'))); return $menu; }
/** * @expectedException \InvalidArgumentException */ public function testReorderingWithWrongItemNames() { $factory = new MenuFactory(); $menu = new MenuItem('root', $factory); $menu->addChild('c1'); $menu->addChild('c2'); $menu->reorderChildren(array('c1', 'c3')); }
public function visitMenuItem(MenuItem $item) { try { $request = $this->getClonedRequest($item->getUri()); $event = new GetResponseEvent($this->kernel, $request, HttpKernelInterface::MASTER_REQUEST); $this->accessListener->handle($event); } catch (AccessDeniedException $e) { $item->setDisplay(false); } catch (AuthenticationCredentialsNotFoundException $e) { } }
private function addMenu(\Symforce\AdminBundle\Compiler\CacheObject\Menu $tree, \Knp\Menu\MenuItem $menu, $as_root = true) { if ($as_root) { $_menu = $menu; } else { $options = array('attributes' => array(), 'childrenAttributes' => array(), 'linkAttributes' => array(), 'extras' => array()); $label = $this->translator->trans($tree->getLabel(), array(), $tree->getDomain()); if ($tree->admin) { $admin_name = $tree->getName(); if (!$this->loadder->auth($admin_name, 'list')) { return; } $admin = $this->loadder->getAdminByName($admin_name); $options['uri'] = $admin->path('list'); } else { if ($tree->getUrl()) { $options['uri'] = $tree->getUrl(); } else { if ($tree->getRouteName()) { $options['uri'] = $this->container->get('router')->generate($tree->getRouteName()); } else { $options['uri'] = 'javascript:alert(0)'; } } } if ($tree->hasChildren()) { if ($menu->isRoot()) { $options['attributes']['class'] = 'dropdown'; $options['extras']['caret'] = true; } else { $options['attributes']['class'] = 'dropdown-submenu'; } $options['childrenAttributes']['class'] = 'dropdown-menu bottom-down'; $options['linkAttributes']['class'] = 'dropdown-toggle'; $options['linkAttributes']['data-toggle'] = 'dropdown'; } if (!$menu->isRoot()) { if (null !== $tree->getDivider()) { if ($tree->getDivider()) { $options['extras']['after_divider'] = true; } else { $options['extras']['before_divider'] = true; } } } if (null !== $tree->getIcon()) { $options['extras']['icon'] = $tree->getIcon(); } $_menu = $menu->addChild($label, $options); } foreach ($tree->getChildren() as $child) { $this->addMenu($child, $_menu, false); } }
public function testGetCurrentMenuItem() { $item = new MenuItem('testItem', $this->factory); $goodItem = new MenuItem('goodItem', $this->factory); $subItem = new MenuItem('subItem', $this->factory); $goodItem->addChild($subItem); $params = array('testItem' => false, 'goodItem' => false, 'subItem' => true); $this->matcher->expects($this->any())->method('isCurrent')->with($this->logicalOr($this->equalTo($item), $this->equalTo($goodItem), $this->equalTo($subItem)))->will($this->returnCallback(function ($param) use(&$params) { return $params[$param->getLabel()]; })); $this->assertEquals($subItem, $this->manager->getCurrentMenuItem(array($item, $goodItem))); }
/** * @param MenuItem $menu * @param PageInterface $parent */ protected function populateChildren(MenuItem $menu, PageInterface $parent) { foreach ($parent as $child) { if (!$child->getDisplay()) { continue; } $item = $this->factory->createItem($child->getTitle(), ['route' => $child->getRoute()]); // Store Page ID $item->setExtra('_page_id', $child->getId()); $menu->addChild($item); $this->populateChildren($item, $child); } }
public function get($name, array $options = array()) { //get the internal pages of the website $menu = parent::get($name, $options); if ($name === 'simple') { //Home menu item $item = new MenuItem('Home', $this->factory); $item->setUri($menu->getUri()); $menu->addChild($item); $item->moveToFirstPosition(); } return $menu; }
protected function setUp() { $factory = new MenuFactory(); $this->menu = $factory->createItem('Root li', array('childrenAttributes' => array('class' => 'root'))); $this->pt1 = $this->menu->addChild('Parent 1'); $this->ch1 = $this->pt1->addChild('Child 1'); $this->ch2 = $this->pt1->addChild('Child 2'); // add the 3rd child via addChild with an object $this->ch3 = new MenuItem('Child 3', $factory); $this->pt1->addChild($this->ch3); $this->pt2 = $this->menu->addChild('Parent 2'); $this->ch4 = $this->pt2->addChild('Child 4'); $this->gc1 = $this->ch4->addChild('Grandchild 1'); }
/** * @param MenuItem $menu * @param array $extras * * @return MenuItem */ public function buildAdminMenu(MenuItem $menu, array $extras = ['beforeCode' => '<i class="fa fa-angle-right"></i>']) { if ($this->hasAdmin()) { $extras = ['afterCode' => '<i class="fa fa-angle-left pull-right"></i>', 'beforeCode' => '<i class="fa fa-cubes"></i>']; $submenu = $menu->addChild($this->getShortName(), ['uri' => $this->container->get('router')->generate('cms_admin_index') . $this->getShortName() . '/'])->setAttribute('class', 'treeview')->setExtras($extras); $submenu->setChildrenAttribute('class', 'treeview-menu'); /** @var \Doctrine\ORM\EntityManager $em */ $em = $this->container->get('doctrine.orm.entity_manager'); foreach ($em->getRepository('UnicatModule:UnicatConfiguration')->findAll() as $uc) { $submenu->addChild($uc->getTitle(), ['route' => 'unicat_admin.configuration', 'routeParameters' => ['configuration' => $uc->getName()]])->setExtras(['beforeCode' => '<i class="fa fa-angle-right"></i>']); } } return $menu; }
public function testLabelTranslationDomainOverride() { $translatorProphecy = $this->prophesize('Symfony\\Component\\Translation\\TranslatorInterface'); $translatorProphecy->trans('some-label', array(), 'my_local_domain', null)->willReturn('my-translation'); $translatorProphecy->trans('some-other-label', array(), 'my_global_domain', null)->willReturn('my-other-translation'); $this->translator = $translatorProphecy->reveal(); $factory = new MenuFactory(); $menu = new MenuItem('test-menu', $factory); $menu->setExtra('translation_domain', 'my_global_domain'); $menu->addChild('some-label', array('uri' => '/whatever'))->setExtra('translation_domain', 'my_local_domain'); $menu->addChild('some-other-label', array('uri' => '/whatever')); $html = $this->renderMenu($menu); $this->assertContains('my-translation', $html); $this->assertContains('my-other-translation', $html); }
/** * @dataProvider navigationConfigureDataProvider * * @param array $settings * @param bool $isEnabled * @param bool $expectedResult */ public function testOnNavigationConfigure($settings, $isEnabled, $expectedResult) { $factory = new MenuFactory(); $this->resolver->expects($this->any())->method('resolve')->will($this->returnArgument(0)); $this->state->expects($this->once())->method('isEntityEnabled')->will($this->returnValue($isEnabled)); $settingsProvider = new SettingsProvider($settings, $this->resolver); $listener = new NavigationListener($settingsProvider, $this->state); $menu = new MenuItem('test_menu', $factory); $salesTab = new MenuItem('sales_tab', $factory); $salesTab->addChild('test_item')->setDisplay(false); $menu->addChild($salesTab); $this->assertFalse($salesTab->getChild('test_item')->isDisplayed()); $eventData = new ConfigureMenuEvent($factory, $menu); $listener->onNavigationConfigure($eventData); $this->assertEquals($expectedResult, $salesTab->getChild('test_item')->isDisplayed()); }
/** * @param \Knp\Menu\ItemInterface|string|array $child * @param array $options * * @return \Knp\Menu\ItemInterface|string */ public function addChild($child, array $options = []) { if (is_array($child) && empty($options)) { list($child, $options) = $child; } return parent::addChild($child, $options); }
public function addChild($child, array $options = []) { if (!empty($options['route'])) { $options['uri'] = ['_name' => $options['route']]; unset($options['route']); } return parent::addChild($child, $options); }
/** * 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 testPrettyRendering() { $menu = new MenuItem('Root li', new MenuFactory()); $menu->setChildrenAttributes(array('class' => 'root')); $menu->addChild('Parent 1'); $menu->addChild('Parent 2'); $renderer = new ListRenderer(); $rendered = <<<HTML <ul class="root"> <li class="first"> <span>Parent 1</span> </li> <li class="last"> <span>Parent 2</span> </li> </ul> HTML; $this->assertEquals($rendered, $renderer->render($menu)); }
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'); } } }
public function visitMenuItem(MenuItem $item) { $domain = $item->getExtra('translation_domain'); $parameters = $item->getExtra('translation_parameters'); if (false === $parameters) { return; } elseif (!is_array($parameters)) { $parameters = []; } $id = $item->getLabel(); if (null !== ($number = $item->getExtra('translation_number'))) { $item->setLabel($this->translator->transChoice($id, $number, $parameters, $domain)); } else { $item->setLabel($this->translator->trans($id, $parameters, $domain)); } }
/** * @dataProvider getBreadcrumbManagerDataProvider * @param $expected * @param $menu * @param $route */ public function testGetBreadcrumbLabels($expected, $menu, $route) { $this->provider->expects($this->any())->method('get')->will($this->returnCallback(function ($menu) { switch ($menu) { case BreadcrumbManager::FRONTEND_MENU: $item = new MenuItem('frontend_menu__test', $this->factory); $item->setExtra('routes', ['another_route', '/another_route/', 'another*route', 'route_with_frontend_true']); $item1 = new MenuItem('frontend_menu__test1', $this->factory); $item2 = new MenuItem('frontend_menu__sub_item', $this->factory); $item1->addChild($item2); $item1->setExtra('routes', []); $item2->addChild($item); return $item1; case 'test_menu': $item = new MenuItem('test_menu__test', $this->factory); $item->setExtra('routes', ['another_route', '/another_route/', 'another*route', 'route_without_frontend']); $item1 = new MenuItem('test_menu__test1', $this->factory); $item2 = new MenuItem('test_menu__sub_item', $this->factory); $item1->addChild($item2); $item1->setExtra('routes', []); $item2->addChild($item); return $item1; } return null; })); $this->assertEquals($expected, $this->manager->getBreadcrumbLabels($menu, $route)); }
private function buildItems(Menu $menu, MenuItem $knpMenu) { $locale = $this->bigfootContext->get('language'); /** * @var Item $item */ foreach ($menu->getItems() as $item) { $itemParameters = array('label' => $item->getLabel()); $link = $item->getLink(); if (isset($link['name'])) { $itemParameters['route'] = $link['name']; if ($this->router instanceof \BeSimple\I18nRoutingBundle\Routing\Router and $this->router->getRouteCollection()->get(sprintf('%s.%s', $link['name'], $locale))) { $parameters['locale'] = $locale; $options = $this->router->getRouteCollection()->get($link['name'] . '.' . $locale)->getOptions(); } else { $options = $this->router->getRouteCollection()->get($link['name'])->getOptions(); } if (isset($options['label'])) { $parameters = array(); $iParameters = $link['parameters']; if (isset($options['parameters'])) { foreach ($options['parameters'] as $key => $parameter) { if (preg_match('/Bundle/i', $parameter['type'])) { $entity = $this->entityManager->getRepository($parameter['type'])->find($iParameters[$parameter['name']]); $method = 'get' . ucfirst($parameter['field']); $parameters[$parameter['name']] = $entity->{$method}(); if (isset($parameter['childs'])) { foreach ($parameter['childs'] as $child) { $method = 'get' . ucfirst($child); $parameters[$child] = $entity->{$method}()->getSlug(); } } } else { $parameters[$parameter['name']] = $iParameters[$parameter['name']]; } } } $itemParameters['routeParameters'] = $parameters; $itemParameters['routeAbsolute'] = true; } } else { $itemParameters['uri'] = isset($link['externalLink']) ? $link['externalLink'] : '#'; } $child = $knpMenu->addChild($item->getSlug(), $itemParameters); foreach ($item->getAttributesByType(Attribute::LINK) as $attribute) { $child->setLinkAttribute($attribute->getName(), $attribute->getValue()); } } return $menu; }
/** ************************************************************************ * Add menu child "Delete" to $menu * @param \Knp\Menu\MenuItem $menu * @param array $options **************************************************************************/ protected function addChildDelete(MenuItem $menu, array $options) { $menu->addChild('Delete', array('uri' => '#'))->setAttribute('icon', 'glyphicon-trash')->setLinkAttribute('data-toggle', 'modal')->setLinkAttribute('data-target', '#confirm-delete')->setLinkAttribute('data-href', $this->container->get('router')->generate('finance_operation_stakeholder_delete', array('stakeholder_id' => $options['stakeholder']->getId()))); }
/** ************************************************************************ * Add menu child "Delete" to $menu * @param \Knp\Menu\MenuItem $menu * @param array $options **************************************************************************/ protected function addChildDelete(MenuItem $menu, array $options) { $menu->addChild('Delete', array('uri' => '#'))->setAttribute('icon', 'glyphicon-trash')->setLinkAttribute('data-toggle', 'modal')->setLinkAttribute('data-target', '#confirm-delete')->setLinkAttribute('data-href', $this->container->get('router')->generate('nutri_recipe_shoplist_delete', array('shoplist_id' => $options['shoplist']->getId()))); }
protected function addChildWithExternalUrl() { $this->menu->addChild('child', array('uri' => 'http://www.symfony-reloaded.org')); }
/** * @param MenuItem $item * * @return bool */ protected function isItemAllowed(MenuItem $item) { return $item->getExtra('isAllowed') && !in_array($item->getUri(), $this->uris) && $item->getUri() !== '#' && $item->isDisplayed(); }
public function testRenderWithCurrentItemNotAsLink() { $menu = new MenuItem('test', new MenuFactory()); $about = $menu->addChild('About', array('uri' => '/about')); $about->setCurrent(true); $rendered = '<ul><li class="current first last"><span>About</span></li></ul>'; $this->assertEquals($rendered, $this->renderer->render($menu, array('currentAsLink' => false))); }
/** * @expectedException LogicException */ public function testAddChildFailsIfInAnotherMenu() { $factory = $this->getMock('Knp\\Menu\\FactoryInterface'); $menu = new MenuItem('Root li', $factory); $child = new MenuItem('Child 3', $factory); $menu->addChild($child); $menu2 = new MenuItem('Second menu', $factory); $menu2->addChild($child); }
/** * Find an item in the menu based on its URI * * @param MenuItem $menuItem * @param $uri * @return MenuItem|null */ private function findItemBasedOnUri(MenuItem $menuItem, $uri) { if ($uri === $menuItem->getUri()) { return $menuItem; } if (!$menuItem->hasChildren()) { return null; } foreach ($menuItem->getChildren() as $child) { $item = $this->findItemBasedOnUri($child, $uri); if ($item !== null) { return $item; } } return null; }