/**
  * Called by the parent menu item to render this menu.
  *
  * This renders the li tag to fit into the parent ul as well as its
  * own nested ul tag if this menu item has children
  *
  * @param \Knp\Menu\ItemInterface $item
  * @param array $options The options to render the item
  * @return string
  */
 protected function renderItem(ItemInterface $item, array $options)
 {
     // if we don't have access or this item is marked to not be shown
     if (!$item->isDisplayed()) {
         return '';
     }
     // create an array than can be imploded as a class list
     $class = (array) $item->getAttribute('class');
     if ($item->isCurrent()) {
         $class[] = $options['currentClass'];
     } elseif ($item->isCurrentAncestor()) {
         $class[] = $options['ancestorClass'];
     }
     // retrieve the attributes and put the final class string back on it
     $attributes = $item->getAttributes();
     if (!empty($class)) {
         $attributes['class'] = implode(' ', $class);
     }
     // opening li tag
     $html = $this->format('<li' . $this->renderHtmlAttributes($attributes) . '>', 'li', $item->getLevel(), $options);
     // render the text/link inside the li tag
     $html .= $this->renderLink($item, $options);
     // renders the embedded ul
     $childrenClass = (array) $item->getChildrenAttribute('class');
     $childrenClass[] = 'menu_level_' . $item->getLevel();
     $childrenAttributes = $item->getChildrenAttributes();
     $childrenAttributes['class'] = implode(' ', $childrenClass);
     $html .= $this->renderList($item, $childrenAttributes, $options);
     // closing li tag
     $html .= $this->format('</li>', 'li', $item->getLevel(), $options);
     return $html;
 }
Example #2
0
    /**
     * Called by the parent menu item to render this menu.
     *
     * This renders the li tag to fit into the parent ul as well as its
     * own nested ul tag if this menu item has children
     *
     * @param \Knp\Menu\ItemInterface $item
     * @param array $options The options to render the item
     * @return string
     */
    public function renderItem(ItemInterface $item, array $options = array())
    {
        $options = array_merge($this->getDefaultOptions(), $options);

        // if we don't have access or this item is marked to not be shown
        if (!$item->isDisplayed()) {
            return '';
        }

        // explode the class string into an array of classes
        $class = ($item->getAttribute('class')) ? explode(' ', $item->getAttribute('class')) : array();

        if ($item->isCurrent()) {
            $class[] = $options['currentClass'];
        } elseif ($item->isCurrentAncestor()) {
            $class[] = $options['ancestorClass'];
        }

        if ($item->actsLikeFirst()) {
            $class[] = $options['firstClass'];
        }
        if ($item->actsLikeLast()) {
            $class[] = $options['lastClass'];
        }

        // retrieve the attributes and put the final class string back on it
        $attributes = $item->getAttributes();
        if (!empty($class)) {
            $attributes['class'] = implode(' ', $class);
        }

        // opening li tag
        $html = $this->format('<li'.$this->renderHtmlAttributes($attributes).'>', 'li', $item->getLevel());

        // render the text/link inside the li tag
        //$html .= $this->format($item->getUri() ? $item->renderLink() : $item->renderLabel(), 'link', $item->getLevel());
        $html .= $this->renderLink($item, $options);

        // renders the embedded ul if there are visible children
        if ($item->hasChildren() && 0 !== $options['depth'] && $item->getDisplayChildren()) {

            $childrenClass = ($item->getChildrenAttribute('class')) ? explode(' ', $item->getChildrenAttribute('class')) : array();
            $childrenClass[] = 'menu_level_'.$item->getLevel();

            $childrenAttributes = $item->getChildrenAttributes();
            $childrenAttributes['class'] = implode(' ', $childrenClass);

            $html .= $this->format('<ul'.$this->renderHtmlAttributes($childrenAttributes).'>', 'ul', $item->getLevel());
            $html .= $this->renderChildren($item, $options);
            $html .= $this->format('</ul>', 'ul', $item->getLevel());
        }

        // closing li tag
        $html .= $this->format('</li>', 'li', $item->getLevel());

        return $html;
    }
 /**
  * @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;
 }