0 follower

Final Class Yiisoft\Yii\Widgets\Menu

InheritanceYiisoft\Yii\Widgets\Menu » Yiisoft\Widget\Widget

Menu displays a multi-level menu using nested HTML lists.

The Yiisoft\Yii\Widgets\Menu::items() method specifies the possible items in the menu. A menu item can contain sub-items which specify the sub-menu under that menu item.

Menu checks the current route and request parameters to toggle certain menu items with active state.

Note that Menu only renders the HTML tags about the menu. It does not do any styling. You are responsible to provide CSS styles to make it look like a real menu.

The following example shows how to use Menu:

<?= Menu::Widget()
    ->items([
        ['label' => 'Login', 'link' => 'site/login', 'visible' => true],
    ]);
?>

Public Methods

Hide inherited methods

Method Description Defined By
activateItems() Return new instance with specified whether to activate parent menu items when one of the corresponding child menu items is active. Yiisoft\Yii\Widgets\Menu
activeClass() Returns a new instance with the specified active CSS class. Yiisoft\Yii\Widgets\Menu
afterAttributes() Returns a new instance with the specified after container attributes. Yiisoft\Yii\Widgets\Menu
afterClass() Returns a new instance with the specified after container class. Yiisoft\Yii\Widgets\Menu
afterContent() Returns a new instance with the specified after content. Yiisoft\Yii\Widgets\Menu
afterTag() Returns a new instance with the specified after container tag. Yiisoft\Yii\Widgets\Menu
attributes() Returns a new instance with the HTML attributes. The following special options are recognized. Yiisoft\Yii\Widgets\Menu
beforeAttributes() Returns a new instance with the specified before container attributes. Yiisoft\Yii\Widgets\Menu
beforeClass() Returns a new instance with the specified before container class. Yiisoft\Yii\Widgets\Menu
beforeContent() Returns a new instance with the specified before content. Yiisoft\Yii\Widgets\Menu
beforeTag() Returns a new instance with the specified before container tag. Yiisoft\Yii\Widgets\Menu
class() Returns a new instance with the specified the class menu widget. Yiisoft\Yii\Widgets\Menu
container() Returns a new instance with the specified enable or disable the container widget. Yiisoft\Yii\Widgets\Menu
currentPath() Returns a new instance with the specified the current path. Yiisoft\Yii\Widgets\Menu
disabledClass() Returns a new instance with the specified disabled CSS class. Yiisoft\Yii\Widgets\Menu
firstItemClass() Returns a new instance with the specified first item CSS class. Yiisoft\Yii\Widgets\Menu
iconContainerAttributes() Returns a new instance with the specified icon container attributes. Yiisoft\Yii\Widgets\Menu
items() List of items in the nav widget. Each array element represents a single menu item which can be either a string or an array with the following structure: Yiisoft\Yii\Widgets\Menu
itemsContainer() Returns a new instance with the specified if enabled or disabled the items' container. Yiisoft\Yii\Widgets\Menu
itemsContainerAttributes() Returns a new instance with the specified items' container attributes. Yiisoft\Yii\Widgets\Menu
itemsContainerClass() Returns a new instance with the specified items' container class. Yiisoft\Yii\Widgets\Menu
itemsTag() Returns a new instance with the specified items tag. Yiisoft\Yii\Widgets\Menu
lastItemClass() Returns a new instance with the specified last item CSS class. Yiisoft\Yii\Widgets\Menu
linkAttributes() Returns a new instance with the specified link attributes. Yiisoft\Yii\Widgets\Menu
linkClass() Returns a new instance with the specified link css class. Yiisoft\Yii\Widgets\Menu
linkTag() Returns a new instance with the specified link tag. Yiisoft\Yii\Widgets\Menu
render() Renders the menu. Yiisoft\Yii\Widgets\Menu
tagName() Returns a new instance with the specified tag for rendering the menu. Yiisoft\Yii\Widgets\Menu
template() Returns a new instance with the specified the template used to render the main menu. Yiisoft\Yii\Widgets\Menu

Method Details

Hide inherited methods

activateItems() public method

Return new instance with specified whether to activate parent menu items when one of the corresponding child menu items is active.

public activateItems( boolean $value ): self
$value boolean

The value to be assigned to the activateItems property.

                public function activateItems(bool $value): self
{
    $new = clone $this;
    $new->activateItems = $value;
    return $new;
}

            
activeClass() public method

Returns a new instance with the specified active CSS class.

public activeClass( string $value ): self
$value string

The CSS class to be appended to the active menu item.

                public function activeClass(string $value): self
{
    $new = clone $this;
    $new->activeClass = $value;
    return $new;
}

            
afterAttributes() public method

Returns a new instance with the specified after container attributes.

public afterAttributes( array $valuesMap ): self
$valuesMap array

Attribute values indexed by attribute names.

                public function afterAttributes(array $valuesMap): self
{
    $new = clone $this;
    $new->afterAttributes = $valuesMap;
    return $new;
}

            
afterClass() public method

Returns a new instance with the specified after container class.

public afterClass( string $value ): self
$value string

The class name.

                public function afterClass(string $value): self
{
    $new = clone $this;
    Html::addCssClass($new->afterAttributes, $value);
    return $new;
}

            
afterContent() public method

Returns a new instance with the specified after content.

public afterContent( string|\Stringable $content ): self
$content string|\Stringable

The content.

                public function afterContent(string|Stringable $content): self
{
    $new = clone $this;
    $new->afterContent = (string) $content;
    return $new;
}

            
afterTag() public method

Returns a new instance with the specified after container tag.

public afterTag( string $value ): self
$value string

The after container tag.

                public function afterTag(string $value): self
{
    $new = clone $this;
    $new->afterTag = $value;
    return $new;
}

            
attributes() public method

Returns a new instance with the HTML attributes. The following special options are recognized.

public attributes( array $valuesMap ): self
$valuesMap array

Attribute values indexed by attribute names.

                public function attributes(array $valuesMap): self
{
    $new = clone $this;
    $new->attributes = $valuesMap;
    return $new;
}

            
beforeAttributes() public method

Returns a new instance with the specified before container attributes.

public beforeAttributes( array $valuesMap ): self
$valuesMap array

Attribute values indexed by attribute names.

                public function beforeAttributes(array $valuesMap): self
{
    $new = clone $this;
    $new->beforeAttributes = $valuesMap;
    return $new;
}

            
beforeClass() public method

Returns a new instance with the specified before container class.

public beforeClass( string $value ): self
$value string

The before container class.

                public function beforeClass(string $value): self
{
    $new = clone $this;
    Html::addCssClass($new->beforeAttributes, $value);
    return $new;
}

            
beforeContent() public method

Returns a new instance with the specified before content.

public beforeContent( string|\Stringable $value ): self
$value string|\Stringable

The content.

                public function beforeContent(string|Stringable $value): self
{
    $new = clone $this;
    $new->beforeContent = (string) $value;
    return $new;
}

            
beforeTag() public method

Returns a new instance with the specified before container tag.

public beforeTag( string $value ): self
$value string

The before container tag.

                public function beforeTag(string $value): self
{
    $new = clone $this;
    $new->beforeTag = $value;
    return $new;
}

            
class() public method

Returns a new instance with the specified the class menu widget.

public class( string $value ): self
$value string

The class menu widget.

                public function class(string $value): self
{
    $new = clone $this;
    Html::addCssClass($new->attributes, $value);
    return $new;
}

            
container() public method

Returns a new instance with the specified enable or disable the container widget.

public container( boolean $value ): self
$value boolean

The container widget enable or disable, for default is true.

                public function container(bool $value): self
{
    $new = clone $this;
    $new->container = $value;
    return $new;
}

            
currentPath() public method

Returns a new instance with the specified the current path.

public currentPath( string $value ): self
$value string

The current path.

                public function currentPath(string $value): self
{
    $new = clone $this;
    $new->currentPath = $value;
    return $new;
}

            
disabledClass() public method

Returns a new instance with the specified disabled CSS class.

public disabledClass( string $value ): self
$value string

The CSS class to be appended to the disabled menu item.

                public function disabledClass(string $value): self
{
    $new = clone $this;
    $new->disabledClass = $value;
    return $new;
}

            
firstItemClass() public method

Returns a new instance with the specified first item CSS class.

public firstItemClass( string $value ): self
$value string

The CSS class that will be assigned to the first item in the main menu or each submenu.

                public function firstItemClass(string $value): self
{
    $new = clone $this;
    $new->firstItemClass = $value;
    return $new;
}

            
iconContainerAttributes() public method

Returns a new instance with the specified icon container attributes.

public iconContainerAttributes( array $valuesMap ): self
$valuesMap array

Attribute values indexed by attribute names.

                public function iconContainerAttributes(array $valuesMap): self
{
    $new = clone $this;
    $new->iconContainerAttributes = $valuesMap;
    return $new;
}

            
items() public method

List of items in the nav widget. Each array element represents a single menu item which can be either a string or an array with the following structure:

  • label: string, required, the nav item label.
  • active: bool, whether the item should be on active state or not.
  • disabled: bool, whether the item should be on disabled state or not. For default disabled is false.
  • encode: bool, whether the label should be HTML encoded or not. For default encodeLabel is true.
  • items: array, optional, the item's submenu items. The structure is the same as for items option.
  • itemsContainerAttributes: array, optional, the HTML attributes for the item's submenu container.
  • link: string, the item's href. Defaults to "#". For default link is "#".
  • linkAttributes: array, the HTML attributes of the item's link. For default linkAttributes is [].
  • icon: string, the item's icon. For default is ``.
  • iconAttributes: array, the HTML attributes of the item's icon. For default iconAttributes is [].
  • iconClass: string, the item's icon CSS class. For default is ``.
  • visible: bool, optional, whether this menu item is visible. Defaults to true.

If a menu item is a string, it will be rendered directly without HTML encoding.

public items( array $valuesMap ): self
$valuesMap array

The list of items to be rendered.

                public function items(array $valuesMap): self
{
    $new = clone $this;
    $new->items = $valuesMap;
    return $new;
}

            
itemsContainer() public method

Returns a new instance with the specified if enabled or disabled the items' container.

public itemsContainer( boolean $value ): self
$value boolean

The items container enable or disable, for default is true.

                public function itemsContainer(bool $value): self
{
    $new = clone $this;
    $new->itemsContainer = $value;
    return $new;
}

            
itemsContainerAttributes() public method

Returns a new instance with the specified items' container attributes.

public itemsContainerAttributes( array $valuesMap ): self
$valuesMap array

Attribute values indexed by attribute names.

                public function itemsContainerAttributes(array $valuesMap): self
{
    $new = clone $this;
    $new-> itemsContainerAttributes = $valuesMap;
    return $new;
}

            
itemsContainerClass() public method

Returns a new instance with the specified items' container class.

public itemsContainerClass( string $value ): self
$value string

The CSS class that will be assigned to the items' container.

                public function itemsContainerClass(string $value): self
{
    $new = clone $this;
    Html::addCssClass($new->itemsContainerAttributes, $value);
    return $new;
}

            
itemsTag() public method

Returns a new instance with the specified items tag.

public itemsTag( string $value ): self
$value string

The tag that will be used to wrap the items.

                public function itemsTag(string $value): self
{
    $new = clone $this;
    $new->itemsTag = $value;
    return $new;
}

            
lastItemClass() public method

Returns a new instance with the specified last item CSS class.

public lastItemClass( string $value ): self
$value string

The CSS class that will be assigned to the last item in the main menu or each submenu.

                public function lastItemClass(string $value): self
{
    $new = clone $this;
    $new->lastItemClass = $value;
    return $new;
}

            
linkAttributes() public method

Returns a new instance with the specified link attributes.

public linkAttributes( array $valuesMap ): self
$valuesMap array

Attribute values indexed by attribute names.

                public function linkAttributes(array $valuesMap): self
{
    $new = clone $this;
    $new->linkAttributes = $valuesMap;
    return $new;
}

            
linkClass() public method

Returns a new instance with the specified link css class.

public linkClass( string $value ): self
$value string

The CSS class that will be assigned to the link.

                public function linkClass(string $value): self
{
    $new = clone $this;
    $new->linkClass = $value;
    return $new;
}

            
linkTag() public method

Returns a new instance with the specified link tag.

public linkTag( string $value ): self
$value string

The tag that will be used to wrap the link.

                public function linkTag(string $value): self
{
    $new = clone $this;
    $new->linkTag = $value;
    return $new;
}

            
render() public method

Renders the menu.

public render( ): string
return string

The result of Widget execution to be outputted.

throws \Yiisoft\Definitions\Exception\CircularReferenceException|\Yiisoft\Definitions\Exception\InvalidConfigException|\Yiisoft\Factory\NotFoundException|\Yiisoft\Definitions\Exception\NotInstantiableException

                public function render(): string
{
    if ($this->items === []) {
        return '';
    }
    /**
     * @psalm-var array<
     *   array-key,
     *   array{
     *     label: string,
     *     link: string,
     *     linkAttributes: array,
     *     active: bool,
     *     disabled: bool,
     *     visible: bool,
     *     items?: array
     *   }
     * > $items
     */
    $items = Helper\Normalizer::menu(
        $this->items,
        $this->currentPath,
        $this->activateItems,
        $this->iconContainerAttributes,
    );
    return $this->renderMenu($items);
}

            
tagName() public method

Returns a new instance with the specified tag for rendering the menu.

public tagName( string $value ): self
$value string

The tag for rendering the menu.

                public function tagName(string $value): self
{
    $new = clone $this;
    $new->tagName = $value;
    return $new;
}

            
template() public method

Returns a new instance with the specified the template used to render the main menu.

public template( string $value ): self
$value string

The template used to render the main menu. In this template, the token {items} will be replaced.

                public function template(string $value): self
{
    $new = clone $this;
    $new->template = $value;
    return $new;
}