Skip to content

Drupal\helper\Menu

Provides helpers working with menus and menu links.

Methods

Name Description
__construct File constructor.
addItemAttributes Adds options/attributes to all menu items (the
  • tags).
  • 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.

    Description

    public __construct (\Drupal\Core\Menu\MenuLinkTreeInterface $menu_tree, \Drupal\Core\Menu\MenuActiveTrailInterface $menu_active_trail)
    

    File constructor.

    Parameters

    • (\Drupal\Core\Menu\MenuLinkTreeInterface) $menu_tree : The entity type manager.
    • (\Drupal\Core\Menu\MenuActiveTrailInterface) $menu_active_trail : The active trail service.

    Return Values

    void


    Description

    public addItemAttributes (array $links, array|\Drupal\Core\Template\Attribute $attributes, int $depth)
    

    Adds options/attributes to all menu items (the

  • tags).

    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


    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


    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.


    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.


    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.


    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.


    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.


    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.


    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.