interface MenuLinkTreeInterface (View source)

Defines an interface for loading, transforming and rendering menu link trees.

The main purposes of this interface are:

  • Load a list of menu links, given a menu name, using MenuLinkTreeInterface::load(). Loaded menu links are returned as a tree by looking at the links' tree meta-data.
  • Which links are loaded can be specified in the menu link tree parameters that are passed to the load() method. You can build your own set of parameters, or you can start from typical defaults by calling the MenuLinkTreeInterface::getCurrentRouteMenuTreeParameters() method. See \Drupal\Core\Menu\MenuTreeParameters for more on menu tree parameters.
  • Transform a menu link tree, by calling MenuLinkTreeInterface::transform(). Examples include access checking, adding custom classes, extracting a subtree depending on the active trail, etc. Note that translation is not a tree transformation, because menu links themselves are responsible for translation. Transformations are performed by "menu link tree manipulators", which are functions or methods; see \Drupal\Core\Menu\DefaultMenuLinkTreeManipulators for examples.
  • Create a render array using MenuLinkTreeInterface::build().

Methods

getCurrentRouteMenuTreeParameters(string $menu_name)

Gets the link tree parameters for rendering a specific menu.

load(string $menu_name, MenuTreeParameters $parameters)

Loads a menu tree with a menu link plugin instance at each element.

transform(array $tree, array $manipulators)

Applies menu link tree manipulators to transform the given tree.

array
build(array $tree)

Builds a renderable array from a menu tree.

int
maxDepth()

Returns the maximum depth of tree that is supported.

int
getSubtreeHeight(string $id)

Finds the height of a subtree rooted by of the given ID.

array
getExpanded(string $menu_name, array $parents)

Finds expanded links in a menu given a set of possible parents.

Details

MenuTreeParameters getCurrentRouteMenuTreeParameters(string $menu_name)

Gets the link tree parameters for rendering a specific menu.

Builds menu link tree parameters that:

  • Expand all links in the active trail based on route being viewed.
  • Expand the descendants of the links in the active trail whose 'expanded' flag is enabled.

This only sets the (relatively complex) parameters to achieve the two above goals, but you can still further customize these parameters.

Parameters

string $menu_name

The menu name, needed for retrieving the active trail and links with the 'expanded' flag enabled.

Return Value

MenuTreeParameters

The parameters to determine which menu links to be loaded into a tree.

See also

MenuTreeParameters

MenuLinkTreeElement[] load(string $menu_name, MenuTreeParameters $parameters)

Loads a menu tree with a menu link plugin instance at each element.

Parameters

string $menu_name

The name of the menu.

MenuTreeParameters $parameters

The parameters to determine which menu links to be loaded into a tree.

Return Value

MenuLinkTreeElement[]

A menu link tree.

MenuLinkTreeElement[] transform(array $tree, array $manipulators)

Applies menu link tree manipulators to transform the given tree.

Parameters

array $tree

The menu tree to manipulate.

array $manipulators

The menu link tree manipulators to apply. Each is an array with keys:

  • callable: a callable or a string that can be resolved to a callable by \Drupal\Core\Controller\ControllerResolverInterface::getControllerFromDefinition()
  • args: optional array of arguments to pass to the callable after $tree.

Return Value

MenuLinkTreeElement[]

The manipulated menu link tree.

array build(array $tree)

Builds a renderable array from a menu tree.

The menu item's LI element is given one of the following classes:

  • expanded: The menu item is showing its submenu.
  • collapsed: The menu item has a submenu that is not shown.
  • leaf: The menu item has no submenu.

Parameters

array $tree

A data structure representing the tree, as returned from MenuLinkTreeInterface::load().

Return Value

array

A renderable array.

int maxDepth()

Returns the maximum depth of tree that is supported.

Return Value

int

The maximum depth.

int getSubtreeHeight(string $id)

Finds the height of a subtree rooted by of the given ID.

Parameters

string $id

The ID of an item in the storage.

Return Value

int

Returns the height of the subtree. This will be at least 1 if the ID exists, or 0 if the ID does not exist in the storage.

array getExpanded(string $menu_name, array $parents)

Finds expanded links in a menu given a set of possible parents.

Parameters

string $menu_name

The menu name.

array $parents

One or more parent IDs to match.

Return Value

array

The menu link IDs that are flagged as expanded in this menu.