/** * inject our config and custom matcher. */ public function __construct() { $matcher = new Matcher(); $matcher->addVoter(new UriVoter(app('url')->current())); $matcher->addVoter(new RouteVoter(app('request'))); $options = config('menu.render'); parent::__construct($matcher, $options); }
/** * @return string html */ public function render() { if ($this->getMenu() instanceof ItemInterface === false) { throw new \InvalidArgumentException('Menu needs to conform to the ItemInterface (Knp\\Menu\\ItemInterface)'); } $matcher = new Matcher(); $matcher->addVoter(new UriVoter((string) $this->getBag()->get('url')->getPath())); $renderer = new ListRenderer($matcher); return $renderer->render($this->getMenu()); }
/** * @param NavbarMenuEvent $event */ public function onNavbarMenuInit(NavbarMenuEvent $event) { $menu = $event->getMenu(); $factory = $event->getFactory(); // Home link $item = $factory->createItem($this->translator->trans('Home', array(), 'asf_backend'), array('route' => 'asf_backend_homepage')); $menu->addChild($item); $matcher = new Matcher(); $matcher->addVoter(new RouteVoter($this->request->getCurrentRequest())); $item->setCurrent($matcher->isCurrent($item)); }
/** * Find current menu item * * @param $menu * @return null|ItemInterface */ public function getCurrentMenuItem($menu) { foreach ($menu as $item) { if ($this->matcher->isCurrent($item)) { return $item; } if ($item->getChildren() && ($currentChild = $this->getCurrentMenuItem($item))) { return $currentChild; } } return null; }
/** * @param boolean $value * * @dataProvider provideBoolean */ public function testFirstVoterWins($value) { $item = $this->getMock('Knp\\Menu\\ItemInterface'); $item->expects($this->any())->method('isCurrent')->will($this->returnValue(null)); $voter1 = $this->getMock('Knp\\Menu\\Matcher\\Voter\\VoterInterface'); $voter1->expects($this->once())->method('matchItem')->with($this->equalTo($item))->will($this->returnValue($value)); $voter2 = $this->getMock('Knp\\Menu\\Matcher\\Voter\\VoterInterface'); $voter2->expects($this->never())->method('matchItem'); $matcher = new Matcher(); $matcher->addVoter($voter1); $matcher->addVoter($voter2); $this->assertSame($value, $matcher->isCurrent($item)); }
/** * Constructor. * * @param ContainerInterface $container * @param FactoryInterface $factory */ public function __construct(ContainerInterface $container, FactoryInterface $factory) { $this->container = $container; $this->factory = $factory; $matcher = new Matcher(); try { $request = $this->container->get('request_stack')->getCurrentRequest(); $voter = new RouteVoter($request); $matcher->addVoter($voter); } catch (InactiveScopeException $e) { // We are most probably running from the command line, which means there is no 'request' service. // We just gracefully continue } parent::__construct($matcher, array('allow_safe_labels' => true, 'currentClass' => 'active')); }
/** * Modify menu by adding, removing or editing items. * * @param \Knp\Menu\ItemInterface $menu * @param array $options * @param string|null $alias */ public function build(ItemInterface $menu, array $options = array(), $alias = null) { $maxItems = $this->configOptions->get('oro_navigation.maxItems'); if (!is_null($maxItems)) { // we'll hide current item, so always select +1 item $options['maxItems'] = $maxItems + 1; } parent::build($menu, $options, $alias); $children = $menu->getChildren(); foreach ($children as $child) { if ($this->matcher->isCurrent($child)) { $menu->removeChild($child); break; } } $this->getMenuManipulator()->slice($menu, 0, $maxItems); }
/** * Set the children menu item nodes to be shown only if the node * is current or the parent is a current ancestor * * @param \Knp\Menu\MenuItem $menu */ public function showOnlyCurrentChildren(\Knp\Menu\MenuItem $menu) { $itemIterator = new RecursiveItemIterator($menu->getIterator()); $iterator = new \RecursiveIteratorIterator($itemIterator, \RecursiveIteratorIterator::SELF_FIRST); foreach ($iterator as $menuItem) { /** @var \Knp\Menu\MenuItem $menuItem */ if (!$this->matcher->isCurrent($menuItem) && !$this->matcher->isAncestor($menuItem)) { $menuItem->setDisplayChildren(false); } } }
/** * * @param array $breadcrumbs */ protected function setBreadcrumb($breadcrumbs) { $course = $this->getCourse(); //$session = $this->getSession(); // Adding course breadcrumb. if (!empty($course)) { $courseBreadcrumb = array('name' => \Display::return_icon('home.png') . ' ' . $course->getTitle(), 'url' => array('route' => 'course', 'routeParameters' => array('cidReq' => $course->getCode(), 'id_session' => api_get_session_id()))); array_unshift($breadcrumbs, $courseBreadcrumb); } $app = $this->app; $app['main_breadcrumb'] = function ($app) use($breadcrumbs) { /** @var \Knp\Menu\MenuItem $menu */ $menu = $app['knp_menu.factory']->createItem('root', array('childrenAttributes' => array('class' => 'breadcrumb', 'currentClass' => 'active'))); if (!empty($breadcrumbs)) { foreach ($breadcrumbs as $item) { if (empty($item['url'])) { $item['url'] = array(); } $menu->addChild($item['name'], $item['url']); } } return $menu; }; $matcher = new Matcher(); $voter = new \Knp\Menu\Silex\Voter\RouteVoter(); $voter->setRequest($this->getRequest()); $matcher->addVoter($voter); $renderer = new \Knp\Menu\Renderer\TwigRenderer($this->get('twig'), 'bread.tpl', $matcher); $bread = $renderer->render($this->get('main_breadcrumb'), array('template' => 'default/layout/bread.tpl')); $app['breadcrumbs'] = $bread; }
/** * @param ContentEntity $contentEntity * @param array $options * @return string */ public function render($contentEntity, $options = array()) { $contentMenuItems = $this->cq()->getContentMenuItems($contentEntity); /** * @var \Knp\Menu\MenuItem $rootMenu * @var \Knp\Menu\MenuItem $menu * */ $menu = array(); $rootMenu = $options['factory']->createItem($contentEntity->getSlug()); $rootMenu->setChildrenAttribute('id', $options['id']); $rootMenu->setChildrenAttribute('class', $options['class']); $menu[$contentEntity->getId()] = $rootMenu; /** * @var ContentEntity $content */ foreach ($contentMenuItems as $content) { $menu[$content->getId()] = $menu[$content->getTreeParent()->getId()]->addChild($content->getSlug(), array('uri' => '#')); if (isset($options['emptyTitle']) && $options['emptyTitle'] === true) { $menu[$content->getId()]->setLabel(''); } else { $menu[$content->getId()]->setLabel($content->getTitle()); } $contentMeta = $this->cm()->getContentAllMeta($content); if (isset($contentMeta['linkType']) && $contentMeta['linkType'] == 'route') { if (isset($contentMeta['linkRoute']) && !empty($contentMeta['linkRoute'])) { $linkURL = $this->tp()->getCurrentSkin()->getNavigationRoute($contentMeta['linkRoute']); $menu[$content->getId()]->setUri($linkURL); } } if (isset($contentMeta['linkType']) && $contentMeta['linkType'] == 'link') { if (isset($contentMeta['linkExternal']) && !empty($contentMeta['linkExternal'])) { $menu[$content->getId()]->setUri($contentMeta['linkExternal']); } } if (isset($contentMeta['linkType']) && $contentMeta['linkType'] == 'content') { if (isset($contentMeta['linkContent']) && $contentMeta['linkContent'] instanceof ContentEntity) { $contentLinkURL = $this->cq()->getPublicURL($contentMeta['linkContent']); $menu[$content->getId()]->setUri($contentLinkURL); } } if (isset($contentMeta['linkTarget']) && !empty($contentMeta['linkTarget'])) { $menu[$content->getId()]->setLinkAttribute('target', $contentMeta['linkTarget']); } if (isset($contentMeta['linkClass']) && !empty($contentMeta['linkClass'])) { $menu[$content->getId()]->setLinkAttribute('class', $contentMeta['linkClass']); } } $requestURL = $this->getRequest()->getRequestUri(); $menuTemplate = $this->getContentTemplate($contentEntity); $matcher = new Matcher(); $voter = new UriVoter($requestURL); $matcher->addVoter($voter); $renderer = new TwigRenderer($options['environment'], $menuTemplate, $matcher); return $renderer->render($menu[$contentEntity->getId()]); }
public function __construct(Request $request) { $this->addVoter(new RequestVoter($request)); parent::__construct(); }