class DefaultMenuLinkTreeManipulators (View source)

Provides a couple of menu link tree manipulators.

This class provides menu link tree manipulators to:

  • perform render cached menu-optimized access checking
  • optimized node access checking
  • generate a unique index for the elements in a tree and sorting by it
  • flatten a tree (i.e. a 1-dimensional tree)

Properties

protected AccessManagerInterface $accessManager

The access manager.

protected AccountInterface $account

The current user.

protected EntityTypeManagerInterface $entityTypeManager

The entity type manager.

Methods

__construct(AccessManagerInterface $access_manager, AccountInterface $account, EntityTypeManagerInterface $entity_type_manager)

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

checkAccess(array $tree)

Performs access checks of a menu tree.

checkNodeAccess(array $tree)

Performs access checking for nodes in an optimized way.

collectNodeLinks(array $tree, array $node_links)

Collects the node links in the menu tree.

menuLinkCheckAccess(MenuLinkInterface $instance)

Checks access for one menu link instance.

generateIndexAndSort(array $tree)

Generates a unique index and sorts by it.

flatten(array $tree)

Flattens the tree to a single level.

Details

__construct(AccessManagerInterface $access_manager, AccountInterface $account, EntityTypeManagerInterface $entity_type_manager)

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

Parameters

AccessManagerInterface $access_manager

The access manager.

AccountInterface $account

The current user.

EntityTypeManagerInterface $entity_type_manager

The entity type manager.

MenuLinkTreeElement[] checkAccess(array $tree)

Performs access checks of a menu tree.

Sets the 'access' property to AccessResultInterface objects on menu link tree elements. Descends into subtrees if the root of the subtree is accessible. Inaccessible subtrees are deleted, except the top-level inaccessible link, to be compatible with render caching.

(This means that top-level inaccessible links are not removed; it is up to the code doing something with the tree to exclude inaccessible links, just like MenuLinkTree::build() does. This allows those things to specify the necessary cacheability metadata.)

This is compatible with render caching, because of cache context bubbling: conditionally defined cache contexts (i.e. subtrees that are only accessible to some users) will bubble just like they do for render arrays. This is why inaccessible subtrees are deleted, except at the top-level inaccessible link: if we didn't keep the first (depth-wise) inaccessible link, we wouldn't be able to know which cache contexts would cause those subtrees to become accessible again, thus forcing us to conclude that the subtree is unconditionally inaccessible.

Parameters

array $tree

The menu link tree to manipulate.

Return Value

MenuLinkTreeElement[]

The manipulated menu link tree.

MenuLinkTreeElement[] checkNodeAccess(array $tree)

Performs access checking for nodes in an optimized way.

This manipulator should be added before the generic ::checkAccess() one, because it provides a performance optimization for ::checkAccess().

Parameters

array $tree

The menu link tree to manipulate.

Return Value

MenuLinkTreeElement[]

The manipulated menu link tree.

Collects the node links in the menu tree.

Parameters

array $tree

The menu link tree to manipulate.

array $node_links

Stores references to menu link elements to effectively set access.

protected AccessResultInterface menuLinkCheckAccess(MenuLinkInterface $instance)

Checks access for one menu link instance.

Parameters

MenuLinkInterface $instance

The menu link instance.

Return Value

AccessResultInterface

The access result.

MenuLinkTreeElement[] generateIndexAndSort(array $tree)

Generates a unique index and sorts by it.

Parameters

array $tree

The menu link tree to manipulate.

Return Value

MenuLinkTreeElement[]

The manipulated menu link tree.

MenuLinkTreeElement[] flatten(array $tree)

Flattens the tree to a single level.

Parameters

array $tree

The menu link tree to manipulate.

Return Value

MenuLinkTreeElement[]

The manipulated menu link tree.