MenuLinkTreeInterface
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
Gets the link tree parameters for rendering a specific menu.
Loads a menu tree with a menu link plugin instance at each element.
Applies menu link tree manipulators to transform the given tree.
Builds a renderable array from a menu tree.
Returns the maximum depth of tree that is supported.
Finds the height of a subtree rooted by of the given ID.
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.
MenuLinkTreeElement[]
load(string $menu_name, MenuTreeParameters $parameters)
Loads a menu tree with a menu link plugin instance at each element.
MenuLinkTreeElement[]
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.
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.
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.