コード例 #1
0
    /**
     * Renders menu tree. Internal method.
     *
     * @param MenuItem  $item        Menu item
     * @param integer $depth         The depth of children to render
     * @param boolean $renderAsChild Render with attributes on the li (true) or the ul around the children (false)
     *
     * @return string
     */
    protected function doRender(MenuItem $item, $depth = null, $renderAsChild = false)
    {
        /**
         * Return an empty string if any of the following are true:
         *   a) The menu has no children eligible to be displayed
         *   b) The depth is 0
         *   c) This menu item has been explicitly set to hide its children
         */
        if (!$item->hasChildren() || $depth === 0 || !$item->getShowChildren()) {
            return '';
        }

        if ($renderAsChild) {
            $attributes = array('class' => 'menu_level_'.$item->getLevel());
        }
        else {
            $attributes = $item->getAttributes();
        }

        // render children with a depth - 1
        $childDepth = ($depth === null) ? null : ($depth - 1);

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

        return $html;
    }
コード例 #2
0
    /**
     *
     * @param MenuItem $menuItem
     * @return array
     */
    public function getMenuAttributes(MenuItem $item)
    {
        // if we don't have access or this item is marked to not be shown
        if (!$item->shouldBeRendered()) {
            return;
        }

        $depth = $item->getLevel();
        $maxDepth = $item->getMaxDepth();
            
        $class = array();
        
        if ($maxDepth > 1) {
            $class[] = "deeper";
        }
        $class[] = "level_{$depth}";

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

        return $attributes;
    }