Skip to content
PHP Ecosystem
Explore

MenuItem

API reference › Bootstrap 6 › item › MenuItem

final readonly classyii2\extensions\bootstrap6\item\MenuItem
Uses traitsItemTrait
Sourcesrc/item/MenuItem.php

Represents a Menu item: a link, a button, a submenu, a header, a text, or a divider.

PropertyTypeDescriptionDefined by
$active readonly constructorbool|nullWhether the item is active, or null to let Nav detect it from $url. Default: null.MenuItem
$disabled readonly constructorboolWhether the item is disabled; it takes precedence over $active. Default: false.MenuItem
$encode readonly constructorbool|nullWhether to HTML-encode $label, or null to use the widget encodeLabels. Default: null.MenuItem
$items readonly constructorarray<array-key, MenuItem|string>Items of the nested submenu; the string - renders a divider, any other string is rendered as is. Default: [].MenuItem
$label readonly constructorstring|StringableLabel of a link, button, or submenu toggle, or content of a header or text; empty for a divider.MenuItem
$linkOptions readonly constructorarray<array-key, mixed>HTML attributes of the .menu-item element. Default: [].MenuItem
$options readonly constructorarray<array-key, mixed>HTML attributes of the .submenu wrapper, header, text, or divider. Default: [].MenuItem
$submenuOptions readonly constructorarray<array-key, mixed>HTML attributes of the nested .menu, merged over the widget submenuOptions. Default: [].MenuItem
$type readonly constructoryii2\extensions\bootstrap6\enum\MenuItemTypeKind of the item, which selects the rendered element.MenuItem
$url readonly constructorarray<array-key, mixed>|string|nullURL of a link, processed by Url::to(). Default: null.MenuItem
$visible readonly constructorboolWhether to render the item. Default: true.MenuItem
MethodDescriptionDefined by
button() staticCreates a <button> item.MenuItem
divider() staticCreates an <hr class="menu-divider"> item.MenuItem
fromArray() staticCreates an item from the array configuration documented in Menu::$items.MenuItem
header() staticCreates an <h6 class="menu-header"> item.MenuItem
link() staticCreates a link item.MenuItem
submenu() staticCreates a submenu: a toggle button followed by a nested .menu, inside a .submenu wrapper.MenuItem
text() staticCreates a <span class="menu-text"> item.MenuItem
withActive()Returns a copy with the given active state.MenuItem
withDisabled()Returns a copy with the given disabled state.MenuItem
withEncode()Returns a copy with the given encoding flag.MenuItem
withItems()Returns a copy with the given nested items.MenuItem
withLabel()Returns a copy with the given label or content.MenuItem
withLinkOptions()Returns a copy with the given .menu-item attributes.MenuItem
withOptions()Returns a copy with the given wrapper, header, text, or divider attributes.MenuItem
withSubmenuOptions()Returns a copy with the given nested .menu attributes.MenuItem
withType()Returns a copy with the given kind.MenuItem
withUrl()Returns a copy with the given URL.MenuItem
withVisible()Returns a copy with the given visibility.MenuItem
public static function button(string|Stringable $label): self

Creates a <button> item.

Parameters

ParameterTypeDescription
$labelstring|StringableButton label.

Returns

self

public static function divider(): self

Creates an <hr class="menu-divider"> item.

Returns

self

public static function fromArray(array<array-key, mixed> $config): self

Creates an item from the array configuration documented in Menu::$items.

The divider, header, and text keys select those kinds, in that order; otherwise, a non-empty items key selects a submenu, a url key (even null) a link, and its absence a button. Nested items arrays are converted recursively; strings are kept.

Parameters

ParameterTypeDescription
$configarray<array-key, mixed>Item configuration.

Returns

self

Throws

public static function header(string|Stringable $content): self

Creates an <h6 class="menu-header"> item.

Parameters

ParameterTypeDescription
$contentstring|StringableHeader content.

Returns

self

public static function link(string|Stringable $label, array<array-key, mixed>|string|null $url): self

Creates a link item.

Parameters

ParameterTypeDescription
$labelstring|StringableLink label.
$urlarray<array-key, mixed>|string|nullLink URL, processed by Url::to(); null renders no href.

Returns

self

public static function submenu(string|Stringable $label, array<array-key, array<string, mixed>|yii2\extensions\bootstrap6\item\MenuItem|string> $items): self

Creates a submenu: a toggle button followed by a nested .menu, inside a .submenu wrapper.

Parameters

ParameterTypeDescription
$labelstring|StringableToggle label.
$itemsarray<array-key, array<string, mixed>|MenuItem|string>Items of the nested menu; arrays are converted through fromArray().

Returns

self

public static function text(string|Stringable $content): self

Creates a <span class="menu-text"> item.

Parameters

ParameterTypeDescription
$contentstring|StringableText content.

Returns

self

public function withActive(bool|null $active): self

Returns a copy with the given active state.

Parameters

ParameterTypeDescription
$activebool|null

Returns

self

public function withDisabled(bool $disabled): self

Returns a copy with the given disabled state.

Parameters

ParameterTypeDescription
$disabledbool

Returns

self

public function withEncode(bool|null $encode): self

Returns a copy with the given encoding flag.

Parameters

ParameterTypeDescription
$encodebool|null

Returns

self

public function withItems(array<array-key, array<string, mixed>|yii2\extensions\bootstrap6\item\MenuItem|string> $items): self

Returns a copy with the given nested items.

Parameters

ParameterTypeDescription
$itemsarray<array-key, array<string, mixed>|MenuItem|string>Items of the nested menu; arrays are converted through fromArray().

Returns

self

public function withLabel(string|Stringable $label): self

Returns a copy with the given label or content.

Parameters

ParameterTypeDescription
$labelstring|Stringable

Returns

self

public function withLinkOptions(array<array-key, mixed> $linkOptions): self

Returns a copy with the given .menu-item attributes.

Parameters

ParameterTypeDescription
$linkOptionsarray<array-key, mixed>HTML attributes of the .menu-item element.

Returns

self

public function withOptions(array<array-key, mixed> $options): self

Returns a copy with the given wrapper, header, text, or divider attributes.

Parameters

ParameterTypeDescription
$optionsarray<array-key, mixed>HTML attributes.

Returns

self

public function withSubmenuOptions(array<array-key, mixed> $submenuOptions): self

Returns a copy with the given nested .menu attributes.

Parameters

ParameterTypeDescription
$submenuOptionsarray<array-key, mixed>HTML attributes of the nested .menu.

Returns

self

public function withType(yii2\extensions\bootstrap6\enum\MenuItemType $type): self

Returns a copy with the given kind.

Parameters

ParameterTypeDescription
$typeyii2\extensions\bootstrap6\enum\MenuItemType

Returns

self

public function withUrl(array<array-key, mixed>|string|null $url): self

Returns a copy with the given URL.

Parameters

ParameterTypeDescription
$urlarray<array-key, mixed>|string|nullURL processed by Url::to().

Returns

self

public function withVisible(bool $visible): self

Returns a copy with the given visibility.

Parameters

ParameterTypeDescription
$visiblebool

Returns

self