Drupal\helper\Menu
Provides helpers working with menus and menu links.
Methods
| Name | Description |
|---|---|
| __construct | Menu constructor. |
| addItemAttributes | Adds options/attributes to all menu items (the |
| addLinkOptions | Adds options/attributes to all menu links (the tags). |
| buildLinkFieldAsMenu | Builds a render array of a set of links like a menu. |
| buildLinksAsMenu | Builds a render array of a set of links like a menu. |
| buildMenu | Builds a render array of a menu and its links. |
| convertLinkItemToLink | Convert a link field item into a link. |
| convertLinkItemsToMenuLinks | Convert a link field to an array of menu links. |
| convertLinkToMenuLink | Convert a link into a menu link. |
| convertLinksToMenuLinks | Convert a link field to an array of menu links. |
| getMenuLinkForEntity | Load the preferred menu link for an entity. |
Menu::__construct
Description
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)
Menu constructor.
Parameters
(\Drupal\Core\Menu\MenuLinkTreeInterface) $menuTree:
The entity type manager.(\Drupal\Core\Menu\MenuActiveTrailInterface) $menuActiveTrail:
The active trail service.(\Drupal\Core\Menu\MenuLinkManagerInterface) $menuLinkManager:
The menu link manager.(\Drupal\Core\Extension\ModuleHandlerInterface) $moduleHandler:
The module handler.(\Drupal\Core\Entity\EntityFieldManagerInterface) $entityFieldManager:
The entity field manager.
Return Values
void
Menu::addItemAttributes
Description
public addItemAttributes (array $links, array|\Drupal\Core\Template\Attribute $attributes, int $depth)
Adds options/attributes to all menu items (the
Parameters
(array) $links:
An array of menu links.(array|\Drupal\Core\Template\Attribute) $attributes:
An array of attributes to add to each menu item.(int) $depth:
A limit to the number of levels to recurse down, or zero to recurse
through all levels of links.
Return Values
void
Menu::addLinkOptions
Description
public addLinkOptions (array $links, array $options, int $depth)
Adds options/attributes to all menu links (the tags).
Parameters
(array) $links:
An array of menu links.(array) $options:
An array of link options/attributes to use when calling
\Drupal\Core\Url::mergeOptions().(int) $depth:
A limit to the number of levels to recurse down, or zero to recurse
through all levels of links.
Return Values
void
Menu::buildLinkFieldAsMenu
Description
public buildLinkFieldAsMenu (string $menu_name, \Drupal\Core\Field\FieldItemListInterface $items)
Builds a render array of a set of links like a menu.
Parameters
(string) $menu_name:
The menu name (any value can be provided).(\Drupal\Core\Field\FieldItemListInterface) $items:
A link field item list.
Return Values
array
The render array.
Menu::buildLinksAsMenu
Description
public buildLinksAsMenu (string $menu_name, \Drupal\Core\Link[] $links)
Builds a render array of a set of links like a menu.
Parameters
(string) $menu_name:
The menu name (any value can be provided).(\Drupal\Core\Link[]) $links:
An array of links.
Return Values
array
The render array.
Menu::buildMenu
Description
public buildMenu (string $menu_name, int $level, int $depth, bool $expand_all_items)
Builds a render array of a menu and its links.
Parameters
(string) $menu_name:
The machine name of the menu.(int) $level:
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.(int) $depth:
Number of levels to display, includes the initial level. Zero is
unlimited depth.(bool) $expand_all_items:
Override the option found on each menu link used for expanding children
and instead display the whole menu tree as expanded.
Return Values
array
The render array.
Menu::convertLinkItemToLink
Description
public convertLinkItemToLink (\Drupal\link\Plugin\Field\FieldType\LinkItem $item)
Convert a link field item into a link.
Parameters
(\Drupal\link\Plugin\Field\FieldType\LinkItem) $item:
The link field item to convert.
Return Values
\Drupal\Core\Link
The link.
Menu::convertLinkItemsToMenuLinks
Description
public convertLinkItemsToMenuLinks (\Drupal\Core\Field\FieldItemListInterface $items)
Convert a link field to an array of menu links.
Parameters
(\Drupal\Core\Field\FieldItemListInterface) $items:
The link field items to convert.
Return Values
array
The menu link items, ready for rendering in a menu.
Menu::convertLinkToMenuLink
Description
public convertLinkToMenuLink (\Drupal\Core\Link $link)
Convert a link into a menu link.
Parameters
(\Drupal\Core\Link) $link:
The link to convert.
Return Values
array
The menu link, ready for rendering in a menu.
Menu::convertLinksToMenuLinks
Description
public convertLinksToMenuLinks (\Drupal\Core\Link[] $links)
Convert a link field to an array of menu links.
Parameters
(\Drupal\Core\Link[]) $links:
The links to convert.
Return Values
array
The menu link items, ready for rendering in a menu.
Menu::getMenuLinkForEntity
Description
public getMenuLinkForEntity (\Drupal\Core\Entity\EntityInterface $entity, string|null $menu_name)
Load the preferred menu link for an entity.
Parameters
(\Drupal\Core\Entity\EntityInterface) $entity:
The entity interface.(string|null) $menu_name:
(optional) The preferred menu name.
Return Values
\Drupal\Core\Menu\MenuLinkInterface|null
The menu link if available, NULL otherwise.