コード例 #1
0
 /**
  * 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;
 }
コード例 #2
0
 /**
  * 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);
 }
コード例 #3
0
 /**
  * @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));
 }
コード例 #4
0
 /**
  * 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);
         }
     }
 }
コード例 #5
0
 /**
  * @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));
 }