コード例 #1
0
 /**
  * Render an array or KNP menu as foundation breadcrumb
  *
  * @param ItemInterface|string $menu
  * @param array $options
  * @return mixed
  */
 public function renderAsBreadcrumb($menu, array $options = array())
 {
     $options = array_merge(array('template' => $this->defaultTemplate), $options);
     if (!is_array($menu) && !$menu instanceof ItemInterface) {
         $path = array();
         if (is_array($menu)) {
             if (empty($menu)) {
                 throw new \InvalidArgumentException('The array cannot be empty');
             }
             $path = $menu;
             $menu = array_shift($path);
         }
         $menu = $this->menuHelper->get($menu, $path);
     }
     // Look into the menu to fetch the current item
     $treeIterator = new \RecursiveIteratorIterator(new RecursiveItemIterator(new \ArrayIterator(array($menu))), \RecursiveIteratorIterator::SELF_FIRST);
     $itemFilterIterator = new CurrentItemFilterIterator($treeIterator, $this->matcher);
     $itemFilterIterator->rewind();
     // Watch for a current item
     $current = $itemFilterIterator->current();
     $manipulator = new MenuManipulator();
     if ($current instanceof ItemInterface) {
         // Extract the items for the breadcrumb
         $breadcrumbs = $manipulator->getBreadcrumbsArray($current);
     } else {
         // Current item could not be located, we only send the first item
         $breadcrumbs = $manipulator->getBreadcrumbsArray($menu);
     }
     // Load the template if needed
     if (!$options['template'] instanceof \Twig_Template) {
         $options['template'] = $this->twig->loadTemplate($options['template']);
     }
     return $options['template']->renderBlock('root', array('breadcrumbs' => $breadcrumbs, 'options' => $options));
 }
コード例 #2
0
 /**
  * Retrieves the current item.
  *
  * @param ItemInterface|string $menu
  *
  * @return ItemInterface
  */
 public function getCurrentItem($menu)
 {
     $rootItem = $this->helper->get($menu);
     $currentItem = $this->retrieveCurrentItem($rootItem);
     if (null === $currentItem) {
         $currentItem = $rootItem;
     }
     return $currentItem;
 }
コード例 #3
0
 /**
  * @param ItemInterface|string $menu
  * @param array                $path
  * @param array                $options
  *
  * @return ItemInterface[]
  *
  * @throws \BadMethodCallException   when there is no menu provider and the menu is given by name
  * @throws \LogicException
  * @throws \InvalidArgumentException when the path is invalid
  */
 public function getCurrents($menu, array $path = array(), array $options = array())
 {
     $menu = $this->menuHelper->get($menu, $path, $options);
     $menuIterator = new \RecursiveIteratorIterator(new RecursiveItemIterator(new \ArrayIterator([$menu])), \RecursiveIteratorIterator::SELF_FIRST);
     $currents = [];
     foreach ($menuIterator as $item) {
         /** @var $item ItemInterface */
         if ($this->matcher->isCurrent($item)) {
             $currents[] = $item->getName();
         }
     }
     return $currents;
 }
コード例 #4
0
 /**
  * Renders the Menu with the specified renderer.
  *
  * @param \Knp\Menu\ItemInterface|string|array $menu
  * @param array $options
  * @param string $renderer
  *
  * @throws \InvalidArgumentException
  * @return string
  */
 public function renderMenu($menu, array $options = array(), $renderer = null)
 {
     $options = array_merge(array('template' => $this->menuTemplate, 'currentClass' => 'active', 'ancestorClass' => 'active', 'allow_safe_labels' => true), $options);
     if (!$menu instanceof ItemInterface) {
         $path = array();
         if (is_array($menu)) {
             if (empty($menu)) {
                 throw new \InvalidArgumentException('The array cannot be empty');
             }
             $path = $menu;
             $menu = array_shift($path);
         }
         $menu = $this->helper->get($menu, $path);
     }
     $menu = $this->helper->get($menu, array(), $options);
     if (isset($options['automenu'])) {
         $this->getMenuConverter()->convert($menu, $options);
     }
     return $this->helper->render($menu, $options, $renderer);
 }
コード例 #5
0
 /**
  * Get KnpMenu
  *
  * @param Request $request
  *
  * @return ItemInterface
  */
 public function getKnpMenu(Request $request = null)
 {
     $menuFactory = new MenuFactory();
     $menu = $menuFactory->createItem('root')->setExtra('request', $request);
     foreach ($this->pool->getAdminGroups() as $name => $group) {
         // Check if the menu group is built by a menu provider
         if (isset($group['provider'])) {
             $subMenu = $this->knpHelper->get($group['provider']);
             $menu->addChild($subMenu)->setAttributes(array('icon' => $group['icon'], 'label_catalogue' => $group['label_catalogue']))->setExtra('roles', $group['roles']);
             continue;
         }
         // The menu group is built by config
         $menu->addChild($name, array('label' => $group['label']))->setAttributes(array('icon' => $group['icon'], 'label_catalogue' => $group['label_catalogue']))->setExtra('roles', $group['roles']);
         foreach ($group['items'] as $item) {
             if (array_key_exists('admin', $item) && $item['admin'] != null) {
                 $admin = $this->pool->getInstance($item['admin']);
                 // skip menu item if no `list` url is available or user doesn't have the LIST access rights
                 if (!$admin->hasRoute('list') || !$admin->isGranted('LIST')) {
                     continue;
                 }
                 $label = $admin->getLabel();
                 $route = $admin->generateUrl('list');
                 $translationDomain = $admin->getTranslationDomain();
             } else {
                 $label = $item['label'];
                 $route = $this->router->generate($item['route'], $item['route_params']);
                 $translationDomain = $group['label_catalogue'];
                 $admin = null;
             }
             $menu[$name]->addChild($label, array('uri' => $route))->setExtra('translationdomain', $translationDomain)->setExtra('admin', $admin);
         }
         if (0 === count($menu[$name]->getChildren())) {
             $menu->removeChild($name);
         }
     }
     return $menu;
 }
コード例 #6
0
ファイル: HelperTest.php プロジェクト: nfouka/jobbet_sf2.5
 /**
  * @expectedException InvalidArgumentException
  */
 public function testGetMenuByInvalidPath()
 {
     $rendererProvider = $this->getMock('Knp\\Menu\\Renderer\\RendererProviderInterface');
     $menuProvider = $this->getMock('Knp\\Menu\\Provider\\MenuProviderInterface');
     $child = $this->getMock('Knp\\Menu\\ItemInterface');
     $child->expects($this->any())->method('getChild')->will($this->returnValue(null));
     $menu = $this->getMock('Knp\\Menu\\ItemInterface');
     $menu->expects($this->any())->method('getChild')->with('child')->will($this->returnValue($child));
     $menuProvider->expects($this->once())->method('get')->with('default')->will($this->returnValue($menu));
     $helper = new Helper($rendererProvider, $menuProvider);
     $this->assertSame($child, $helper->get('default', array('child', 'invalid')));
 }
コード例 #7
0
ファイル: MenuHelper.php プロジェクト: dongilbert/mautic
 /**
  * Retrieves an item following a path in the tree.
  *
  * @param \Knp\Menu\ItemInterface|string $menu
  * @param array                          $path
  * @param array                          $options
  *
  * @return \Knp\Menu\ItemInterface
  */
 public function get($menu, array $path = [], array $options = [])
 {
     return $this->helper->get($menu, $path, $options);
 }