class ToolbarMenuLinkTree extends MenuLinkTree (View source)

Extends MenuLinkTree to add specific theme suggestions for the toolbar.

Properties

protected MenuTreeStorageInterface $treeStorage

The menu link tree storage.

from  MenuLinkTree
protected MenuLinkManagerInterface $menuLinkManager

The menu link plugin manager.

from  MenuLinkTree
protected RouteProviderInterface $routeProvider

The route provider to load routes by name.

from  MenuLinkTree
protected MenuActiveTrailInterface $menuActiveTrail

The active menu trail service.

from  MenuLinkTree
protected ControllerResolverInterface $controllerResolver

The controller resolver.

from  MenuLinkTree

Methods

__construct(MenuTreeStorageInterface $tree_storage, MenuLinkManagerInterface $menu_link_manager, RouteProviderInterface $route_provider, MenuActiveTrailInterface $menu_active_trail, ControllerResolverInterface $controller_resolver)

Constructs a \Drupal\Core\Menu\MenuLinkTree object.

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.

createInstances(array $data_tree)

Returns a tree containing of MenuLinkTreeElement based upon tree data.

transform(array $tree, array $manipulators)

Applies menu link tree manipulators to transform the given tree.

array
build(array $tree, $level = 0)

Builds a renderable array from a menu tree.

array
buildItems(array $tree, CacheableMetadata $tree_access_cacheability, CacheableMetadata $tree_link_cacheability)

Builds the #items property for a menu tree's renderable array.

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

__construct(MenuTreeStorageInterface $tree_storage, MenuLinkManagerInterface $menu_link_manager, RouteProviderInterface $route_provider, MenuActiveTrailInterface $menu_active_trail, ControllerResolverInterface $controller_resolver)

Constructs a \Drupal\Core\Menu\MenuLinkTree object.

Parameters

MenuTreeStorageInterface $tree_storage

The menu link tree storage.

MenuLinkManagerInterface $menu_link_manager

The menu link plugin manager.

RouteProviderInterface $route_provider

The route provider to load routes by name.

MenuActiveTrailInterface $menu_active_trail

The active menu trail service.

ControllerResolverInterface $controller_resolver

The controller resolver.

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.

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.

protected MenuLinkTreeElement[] createInstances(array $data_tree)

Returns a tree containing of MenuLinkTreeElement based upon tree data.

This method converts the tree representation as array coming from the tree storage to a tree containing a list of MenuLinkTreeElement[].

Parameters

array $data_tree

The tree data coming from the menu tree storage.

Return Value

MenuLinkTreeElement[]

An array containing the elements of a menu 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, $level = 0)

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().

$level

Return Value

array

A renderable array.

protected array buildItems(array $tree, CacheableMetadata $tree_access_cacheability, CacheableMetadata $tree_link_cacheability)

Builds the #items property for a menu tree's renderable array.

Helper function for ::build().

Parameters

array $tree

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

CacheableMetadata $tree_access_cacheability

Internal use only. The aggregated cacheability metadata for the access results across the entire tree. Used when rendering the root level.

CacheableMetadata $tree_link_cacheability

Internal use only. The aggregated cacheability metadata for the menu links across the entire tree. Used when rendering the root level.

Return Value

array

The value to use for the #items property of a renderable menu.

Exceptions

DomainException

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.