public function render(ItemInterface $item, array $options = array())
 {
     $options = array_merge($this->defaultOptions, $options);
     $translator = $options['translator'];
     $itemIterator = new \Knp\Menu\Iterator\RecursiveItemIterator($item);
     $iterator = new \RecursiveIteratorIterator($itemIterator, \RecursiveIteratorIterator::SELF_FIRST);
     $items = [];
     foreach ($iterator as $item) {
         $translatedLabel = $translator->trans($item->getLabel());
         $id = $item->getName();
         $itemData = ['id' => strtolower($item->getName()), 'name' => $translatedLabel, 'uri' => $item->getUri()];
         $itemData['has_children'] = $item->hasChildren();
         $parentId = $item->getParent()->getName();
         if ($parentId !== $id) {
             $itemData['parent'] = strtolower($parentId);
             if (!isset($items[$parentId]['children'])) {
                 $items[$parentId]['children'] = [];
             }
             $items[$parentId]['children'][] = $itemData;
         }
         if (isset($items[$id])) {
             $items[$id] = array_merge($itemData, $items[$id]);
         } else {
             $items[$id] = $itemData;
         }
     }
     return $items;
 }
 public function render(ItemInterface $item, array $options = array())
 {
     $options = array_merge(array('currentClass' => 'active'), $options);
     if ('root' === $item->getName()) {
         $item->setChildrenAttribute('class', trim('nav navbar-nav ' . $item->getAttribute('class')));
     }
     return parent::render($item, $options);
 }
Example #3
0
 public function addCurrent(ItemInterface $menu)
 {
     $currentRoute = $this->stack->getMasterRequest()->get('_route');
     $route = $this->menu[$menu->getName()]['route'];
     if ($currentRoute == $route) {
         $menu->setCurrent(true);
     }
 }
Example #4
0
 protected function renderSpanElement(ItemInterface $item, array $options)
 {
     return sprintf('<span%s>%s</span>', $this->renderHtmlAttributes($item->getLabelAttributes()), $item->getName());
 }
 /**
  * Set uri, display context to menu item
  *
  * @param  MenuItemInterface $item
  * @param  array             $routeParameters
  * @param  array             $options
  *
  * @return MenuFactoryInterface
  */
 protected function setContext(MenuItemInterface $item, array $routeParameters = array(), array $options = array())
 {
     $display = true;
     $rootItem = !$item->getName();
     $token = $this->security->getToken();
     if ($token) {
         if ($options['roles'] && !$this->security->isGranted($options['roles'])) {
             $display = false;
         }
         if ($display) {
             foreach ((array) $item->getExtra('oids') as $oidItem) {
                 if (!$this->security->isGranted($oidItem['permissions'], $oidItem['oid'])) {
                     $display = false;
                     break;
                 }
             }
         }
     }
     if ($options['route'] && !$rootItem) {
         $acceptedRouteParameters = array_intersect_key($routeParameters, $options['routeAcceptedParameters']);
         if ($options['routeRequiredParameters'] === $options['routeAcceptedParameters']) {
             $uri = $this->router->generate($options['route'], $acceptedRouteParameters, $options['routeAbsolute']);
             $item->setUri($uri);
         } else {
             $display = false;
         }
     }
     if (!$display) {
         if ($options['showNonAuthorized'] && !$token) {
             $display = true;
         }
         if ($options['showAsText']) {
             $display = true;
             $item->setUri(null);
         }
     }
     if (!$display) {
         $item->setDisplay(false);
     }
     return $this;
 }
Example #6
0
    /**
     * Moves child to specified position. Rearange other children accordingly.
     *
     * @param \Knp\Menu\ItemInterface $child Child to move.
     * @param integer $position Position to move child to.
     * @return \Knp\Menu\ItemInterface
     */
    public function moveChildToPosition(ItemInterface $child, $position)
    {
        $name = $child->getName();
        $order = array_keys($this->children);

        $oldPosition = array_search($name, $order);
        unset($order[$oldPosition]);

        $order = array_values($order);

        array_splice($order, $position, 0, $name);
        $this->reorderChildren($order);

        return $this;
    }
 /**
  * @param ItemInterface $item
  * @param integer|null  $depth the depth until which children should be exported (null means unlimited)
  *
  * @return array
  */
 public function toArray(ItemInterface $item, $depth = null)
 {
     $array = array('name' => $item->getName(), 'label' => $item->getLabel(), 'uri' => $item->getUri(), 'attributes' => $item->getAttributes(), 'labelAttributes' => $item->getLabelAttributes(), 'linkAttributes' => $item->getLinkAttributes(), 'childrenAttributes' => $item->getChildrenAttributes(), 'extras' => $item->getExtras(), 'display' => $item->isDisplayed(), 'displayChildren' => $item->getDisplayChildren(), 'current' => $item->isCurrent());
     // export the children as well, unless explicitly disabled
     if (0 !== $depth) {
         $childDepth = null === $depth ? null : $depth - 1;
         $array['children'] = array();
         foreach ($item->getChildren() as $key => $child) {
             $array['children'][$key] = $this->toArray($child, $childDepth);
         }
     }
     return $array;
 }