MenuItem
API reference › Bootstrap 6 › item › MenuItem
Represents a Menu item: a link, a button, a submenu, a header, a text, or a divider.
Properties
Section titled “Properties”| Property | Type | Description | Defined by |
|---|---|---|---|
$active readonly constructor | bool| | Whether the item is active, or null to let Nav detect it from $url. Default: null. | MenuItem |
$disabled readonly constructor | bool | Whether the item is disabled; it takes precedence over $active. Default: false. | MenuItem |
$encode readonly constructor | bool| | Whether to HTML-encode $label, or null to use the widget encodeLabels. Default: null. | MenuItem |
$items readonly constructor | array<array-key, MenuItem| | Items of the nested submenu; the string - renders a divider, any other string is rendered as is. Default: []. | MenuItem |
$label readonly constructor | string|Stringable | Label of a link, button, or submenu toggle, or content of a header or text; empty for a divider. | MenuItem |
$linkOptions readonly constructor | array<array-key, | HTML attributes of the .menu-item element. Default: []. | MenuItem |
$options readonly constructor | array<array-key, | HTML attributes of the .submenu wrapper, header, text, or divider. Default: []. | MenuItem |
$submenuOptions readonly constructor | array<array-key, | HTML attributes of the nested .menu, merged over the widget submenuOptions. Default: []. | MenuItem |
$type readonly constructor | yii2\ | Kind of the item, which selects the rendered element. | MenuItem |
$url readonly constructor | array<array-key, | URL of a link, processed by Url::to(). Default: null. | MenuItem |
$visible readonly constructor | bool | Whether to render the item. Default: true. | MenuItem |
Methods
Section titled “Methods”| Method | Description | Defined by |
|---|---|---|
button() static | Creates a <button> item. | MenuItem |
divider() static | Creates an <hr class="menu-divider"> item. | MenuItem |
fromArray() static | Creates an item from the array configuration documented in Menu::$items. | MenuItem |
header() static | Creates an <h6 class="menu-header"> item. | MenuItem |
link() static | Creates a link item. | MenuItem |
submenu() static | Creates a submenu: a toggle button followed by a nested .menu, inside a .submenu wrapper. | MenuItem |
text() static | Creates 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 |
Method details
Section titled “Method details”button()
Section titled “button() ”public static function button(string|Stringable $label): selfCreates a <button> item.
divider()
Section titled “divider() ”public static function divider(): selfCreates an <hr class="menu-divider"> item.
Returns
self
fromArray()
Section titled “fromArray() ”public static function fromArray(array<array-key, mixed> $config): selfCreates 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
| Parameter | Type | Description |
|---|---|---|
$config | array<array-key, | Item configuration. |
Returns
self
Throws
yii\base\InvalidConfigException: if a link, button, or submenu has nostring|Stringablelabel.
header()
Section titled “header() ”public static function header(string|Stringable $content): selfCreates an <h6 class="menu-header"> item.
link()
Section titled “link() ”public static function link(string|Stringable $label, array<array-key, mixed>|string|null $url): selfCreates a link item.
Parameters
| Parameter | Type | Description |
|---|---|---|
$label | string|Stringable | Link label. |
$url | array<array-key, | Link URL, processed by Url::to(); null renders no href. |
Returns
self
submenu()
Section titled “submenu() ”public static function submenu(string|Stringable $label, array<array-key, array<string, mixed>|yii2\extensions\bootstrap6\item\MenuItem|string> $items): selfCreates a submenu: a toggle button followed by a nested .menu, inside a .submenu wrapper.
Parameters
| Parameter | Type | Description |
|---|---|---|
$label | string|Stringable | Toggle label. |
$items | array<array-key,MenuItem| | Items of the nested menu; arrays are converted through fromArray(). |
Returns
self
text()
Section titled “text() ”public static function text(string|Stringable $content): selfCreates a <span class="menu-text"> item.
withActive()
Section titled “withActive() ”public function withActive(bool|null $active): selfReturns a copy with the given active state.
Parameters
| Parameter | Type | Description |
|---|---|---|
$active | bool| |
Returns
self
withDisabled()
Section titled “withDisabled() ”public function withDisabled(bool $disabled): selfReturns a copy with the given disabled state.
Parameters
| Parameter | Type | Description |
|---|---|---|
$disabled | bool |
Returns
self
withEncode()
Section titled “withEncode() ”public function withEncode(bool|null $encode): selfReturns a copy with the given encoding flag.
Parameters
| Parameter | Type | Description |
|---|---|---|
$encode | bool| |
Returns
self
withItems()
Section titled “withItems() ”public function withItems(array<array-key, array<string, mixed>|yii2\extensions\bootstrap6\item\MenuItem|string> $items): selfReturns a copy with the given nested items.
Parameters
| Parameter | Type | Description |
|---|---|---|
$items | array<array-key,MenuItem| | Items of the nested menu; arrays are converted through fromArray(). |
Returns
self
withLabel()
Section titled “withLabel() ”public function withLabel(string|Stringable $label): selfReturns a copy with the given label or content.
withLinkOptions()
Section titled “withLinkOptions() ”public function withLinkOptions(array<array-key, mixed> $linkOptions): selfReturns a copy with the given .menu-item attributes.
Parameters
| Parameter | Type | Description |
|---|---|---|
$linkOptions | array<array-key, | HTML attributes of the .menu-item element. |
Returns
self
withOptions()
Section titled “withOptions() ”public function withOptions(array<array-key, mixed> $options): selfReturns a copy with the given wrapper, header, text, or divider attributes.
Parameters
| Parameter | Type | Description |
|---|---|---|
$options | array<array-key, | HTML attributes. |
Returns
self
withSubmenuOptions()
Section titled “withSubmenuOptions() ”public function withSubmenuOptions(array<array-key, mixed> $submenuOptions): selfReturns a copy with the given nested .menu attributes.
Parameters
| Parameter | Type | Description |
|---|---|---|
$submenuOptions | array<array-key, | HTML attributes of the nested .menu. |
Returns
self
withType()
Section titled “withType() ”public function withType(yii2\extensions\bootstrap6\enum\MenuItemType $type): selfReturns a copy with the given kind.
withUrl()
Section titled “withUrl() ”public function withUrl(array<array-key, mixed>|string|null $url): selfReturns a copy with the given URL.
Parameters
| Parameter | Type | Description |
|---|---|---|
$url | array<array-key, | URL processed by Url::to(). |
Returns
self
withVisible()
Section titled “withVisible() ”public function withVisible(bool $visible): selfReturns a copy with the given visibility.
Parameters
| Parameter | Type | Description |
|---|---|---|
$visible | bool |
Returns
self