/** * Add content to the ItemList * * @param Content $content Content object * @param ItemList $children Children * @param array $itemAttributes Attributes for the item (li) * @param string $itemElement Element for the item (li is default) * @param string $beforeContent String to add before the content * @param string $afterContent String to add after the content */ public function addContent($content, $children = null, $itemAttributes = array(), $itemElement = null, $beforeContent = null, $afterContent = null) { $item = new Item($this, $content, $children, $itemElement, $beforeContent, $afterContent); $item->setAttributes($itemAttributes); // Set Item as parent of its children if (!is_null($children)) { $children->setParent($item); } $this->setChild($item); return $item; }
public function testCanCreateElementlessItems() { $item = new Item(static::$itemList, static::$link); $item->setElement(null); $this->assertHTML($this->matchLink(), $item->render()); }
/** * Add a new sub-item to the current item. * * @param string $name * @param string|Closure $attributes * @param Closure $callback * @return void */ public function add($name, $attributes = null, $callback = null) { $item = new Item(); // Set the new item's parents. if (!is_null($this->menu)) { $item->setMenu($this->menu); } $item->setParent($this); // Set the new item's attributes. $item->name = $name; if (!is_null($attributes)) { // If the attributes is just a string, then it is the URL // for the menu item. if (is_string($attributes)) { $item->url = $attributes; } elseif ($attributes instanceof Closure) { $attributes($item); } else { throw new \InvalidArgumentException(); } } if ($callback instanceof Closure) { $callback($item); } $this->items[$name] = $item; }
/** * Render an item's sub-items. * * @param Menu\Items\Item $list * @param int $depth * @return string */ public function renderList(Item $list, $depth = 1) { $output = ''; $items = $list->items(); if (!empty($items)) { $output = $this->format('<ul' . $this->attributes($list->ul) . '>', $depth); $itemCount = count($items); foreach ($items as $item) { // If this is the only item of its level, give it the single item class. if ($itemCount == 1) { $this->addClass($item->label, $this->singleClass); } $output .= $this->renderItem($item, $depth + 1); } $output .= $this->format('</ul>', $depth); } return $output; }