Menu
Provides helpers working with menus and menu links.
- Full name:
\Drupal\helper\Menu
Properties
menuTree
protected \Drupal\Core\Menu\MenuLinkTreeInterface $menuTree
menuActiveTrail
protected \Drupal\Core\Menu\MenuActiveTrailInterface $menuActiveTrail
menuLinkManager
protected \Drupal\Core\Menu\MenuLinkManagerInterface $menuLinkManager
moduleHandler
protected \Drupal\Core\Extension\ModuleHandlerInterface $moduleHandler
entityFieldManager
protected \Drupal\Core\Entity\EntityFieldManagerInterface $entityFieldManager
menuUiUtility
protected ?\Drupal\menu_ui\MenuUiUtility $menuUiUtility
Methods
__construct
Menu constructor.
public __construct(\Drupal\Core\Menu\MenuLinkTreeInterface $menuTree, \Drupal\Core\Menu\MenuActiveTrailInterface $menuActiveTrail, \Drupal\Core\Menu\MenuLinkManagerInterface $menuLinkManager, \Drupal\Core\Extension\ModuleHandlerInterface $moduleHandler, \Drupal\Core\Entity\EntityFieldManagerInterface $entityFieldManager, \Drupal\menu_ui\MenuUiUtility|null $menuUiUtility = null): mixed
Parameters:
| Parameter | Type | Description |
|---|---|---|
$menuTree |
\Drupal\Core\Menu\MenuLinkTreeInterface | |
| The entity type manager. | ||
$menuActiveTrail |
\Drupal\Core\Menu\MenuActiveTrailInterface | |
| The active trail service. | ||
$menuLinkManager |
\Drupal\Core\Menu\MenuLinkManagerInterface | |
| The menu link manager. | ||
$moduleHandler |
\Drupal\Core\Extension\ModuleHandlerInterface | |
| The module handler. | ||
$entityFieldManager |
\Drupal\Core\Entity\EntityFieldManagerInterface | |
| The entity field manager. | ||
$menuUiUtility |
\Drupal\menu_ui\MenuUiUtility|null | |
| The menu UI utility. |
buildMenu
Builds a render array of a menu and its links.
public buildMenu(string $menu_name, int $level = 1, int $depth = 0, bool $expand_all_items = false): array
Parameters:
| Parameter | Type | Description |
|---|---|---|
$menu_name |
string | |
| The machine name of the menu. | ||
$level |
int | |
| Initial visibility level. The menu is only visible if the menu item for | ||
| the current page is at this level or below it. Use level 1 to always | ||
| display this menu. | ||
$depth |
int | |
| Number of levels to display, includes the initial level. Zero is | ||
| unlimited depth. | ||
$expand_all_items |
bool | |
| Override the option found on each menu link used for expanding children | ||
| and instead display the whole menu tree as expanded. |
Return Value:
The render array.
See Also:
- \Drupal\system\Plugin\Block\SystemMenuBlock::build
buildLinksAsMenu
Builds a render array of a set of links like a menu.
public buildLinksAsMenu(string $menu_name, \Drupal\Core\Link[] $links): array
Parameters:
| Parameter | Type | Description |
|---|---|---|
$menu_name |
string | |
| The menu name (any value can be provided). | ||
$links |
\Drupal\Core\Link[] | |
| An array of links. |
Return Value:
The render array.
convertLinksToMenuLinks
Convert a link field to an array of menu links.
public convertLinksToMenuLinks(\Drupal\Core\Link[] $links): array
Parameters:
| Parameter | Type | Description |
|---|---|---|
$links |
\Drupal\Core\Link[] | |
| The links to convert. |
Return Value:
The menu link items, ready for rendering in a menu.
convertLinkToMenuLink
Convert a link into a menu link.
public convertLinkToMenuLink(\Drupal\Core\Link $link): array
Parameters:
| Parameter | Type | Description |
|---|---|---|
$link |
\Drupal\Core\Link | |
| The link to convert. |
Return Value:
The menu link, ready for rendering in a menu.
buildLinkFieldAsMenu
Builds a render array of a set of links like a menu.
public buildLinkFieldAsMenu(string $menu_name, \Drupal\Core\Field\FieldItemListInterface $items): array
Parameters:
| Parameter | Type | Description |
|---|---|---|
$menu_name |
string | |
| The menu name (any value can be provided). | ||
$items |
\Drupal\Core\Field\FieldItemListInterface | |
| A link field item list. |
Return Value:
The render array.
convertLinkItemsToMenuLinks
Convert a link field to an array of menu links.
public convertLinkItemsToMenuLinks(\Drupal\Core\Field\FieldItemListInterface $items): array
Parameters:
| Parameter | Type | Description |
|---|---|---|
$items |
\Drupal\Core\Field\FieldItemListInterface | |
| The link field items to convert. |
Return Value:
The menu link items, ready for rendering in a menu.
convertLinkItemToLink
Convert a link field item into a link.
public convertLinkItemToLink(\Drupal\link\Plugin\Field\FieldType\LinkItem $item): \Drupal\Core\Link
Parameters:
| Parameter | Type | Description |
|---|---|---|
$item |
\Drupal\link\Plugin\Field\FieldType\LinkItem | |
| The link field item to convert. |
Return Value:
The link.
addLinkOptions
Adds options/attributes to all menu links (the tags).
public addLinkOptions(array& $links, array $options, int $depth = 0): void
Parameters:
| Parameter | Type | Description |
|---|---|---|
$links |
array | |
| An array of menu links. | ||
$options |
array | |
| An array of link options/attributes to use when calling | ||
| \Drupal\Core\Url::mergeOptions(). | ||
$depth |
int | |
| A limit to the number of levels to recurse down, or zero to recurse | ||
| through all levels of links. |
addItemAttributes
Adds options/attributes to all menu items (the
public addItemAttributes(array& $links, array|\Drupal\Core\Template\Attribute $attributes, int $depth = 0): void
Parameters:
| Parameter | Type | Description |
|---|---|---|
$links |
array | |
| An array of menu links. | ||
$attributes |
array|\Drupal\Core\Template\Attribute | |
| An array of attributes to add to each menu item. | ||
$depth |
int | |
| A limit to the number of levels to recurse down, or zero to recurse | ||
| through all levels of links. |
getMenuLinkForEntity
Load the preferred menu link for an entity.
public getMenuLinkForEntity(\Drupal\Core\Entity\EntityInterface $entity, string|null $menu_name = null): \Drupal\Core\Menu\MenuLinkInterface|null
Parameters:
| Parameter | Type | Description |
|---|---|---|
$entity |
\Drupal\Core\Entity\EntityInterface | |
| The entity interface. | ||
$menu_name |
string|null | |
| (optional) The preferred menu name. |
Return Value:
The menu link if available, NULL otherwise.