class MenuLinkTreeMenuToken in Menu Token 9.1.x
Same name and namespace in other branches
- 8 src/Service/MenuLinkTreeMenuToken.php \Drupal\menu_token\Service\MenuLinkTreeMenuToken
Implements the loading, transforming and rendering of menu link trees.
Hierarchy
- class \Drupal\Core\Menu\MenuLinkTree implements MenuLinkTreeInterface
- class \Drupal\menu_token\Service\MenuLinkTreeMenuToken
Expanded class hierarchy of MenuLinkTreeMenuToken
File
- src/
Service/ MenuLinkTreeMenuToken.php, line 14
Namespace
Drupal\menu_token\ServiceView source
class MenuLinkTreeMenuToken extends MenuLinkTree {
/**
* {@inheritdoc}
*/
public function build(array $tree) {
$tree_access_cacheability = new CacheableMetadata();
$tree_link_cacheability = new CacheableMetadata();
$items = $this
->buildItems($tree, $tree_access_cacheability, $tree_link_cacheability);
$build = [];
// Apply the tree-wide gathered access cacheability metadata and link
// cacheability metadata to the render array. This ensures that the
// rendered menu is varied by the cache contexts that the access results
// and (dynamic) links depended upon, and invalidated by the cache tags
// that may change the values of the access results and links.
$tree_cacheability = $tree_access_cacheability
->merge($tree_link_cacheability);
$tree_cacheability
->applyTo($build);
if ($items) {
// Make sure drupal_render() does not re-order the links.
$build['#sorted'] = TRUE;
// Get the menu name from the last link.
$item = end($items);
$link = $item['original_link'];
$menu_name = $link
->getMenuName();
// Add the theme wrapper for outer markup.
// Allow menu-specific theme overrides.
$build['#theme'] = 'menu__' . strtr($menu_name, '-', '_');
$build['#menu_name'] = $menu_name;
$build['#items'] = $items;
// Set cache tag.
$build['#cache']['tags'][] = 'config:system.menu.' . $menu_name;
$build['#cache']['contexts'] = $tree_cacheability
->getCacheContexts();
}
return $build;
}
/**
* Builds the #items property for a menu tree's renderable array.
*
* Helper function for ::build().
*
* @param \Drupal\Core\Menu\MenuLinkTreeElement[] $tree
* A data structure representing the tree, as returned from
* MenuLinkTreeInterface::load().
* @param \Drupal\Core\Cache\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.
* @param \Drupal\Core\Cache\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 array
* The value to use for the #items property of a renderable menu.
*
* @throws \DomainException
*/
protected function buildItems(array $tree, CacheableMetadata &$tree_access_cacheability, CacheableMetadata &$tree_link_cacheability) {
$items = [];
$current_path = \Drupal::request()
->getRequestUri();
foreach ($tree as $data) {
/** @var \Drupal\Core\Menu\MenuLinkInterface $link */
$link = $data->link;
// Generally we only deal with visible links, but just in case.
if (!$link
->isEnabled()) {
continue;
}
if ($data->access !== NULL && !$data->access instanceof AccessResultInterface) {
throw new \DomainException('MenuLinkTreeElement::access must be either NULL or an AccessResultInterface object.');
}
// Gather the access cacheability of every item in the menu link tree,
// including inaccessible items. This allows us to render cache the menu
// tree, yet still automatically vary the rendered menu by the same cache
// contexts that the access results vary by.
// However, if $data->access is not an AccessResultInterface object, this
// will still render the menu link, because this method does not want to
// require access checking to be able to render a menu tree.
if ($data->access instanceof AccessResultInterface) {
$tree_access_cacheability = $tree_access_cacheability
->merge(CacheableMetadata::createFromObject($data->access));
}
// Gather the cacheability of every item in the menu link tree. Some links
// may be dynamic: they may have a dynamic text (e.g. a "Hi, <user>" link
// text, which would vary by 'user' cache context), or a dynamic route
// name or route parameters.
$tree_link_cacheability = $tree_link_cacheability
->merge(CacheableMetadata::createFromObject($data->link));
$def = $link
->getPluginDefinition();
if (!empty($def["options"]["bubleble_metadata"])) {
$tree_link_cacheability = $tree_link_cacheability
->merge(CacheableMetadata::createFromObject($def["options"]["bubleble_metadata"]));
}
// Only render accessible links.
if ($data->access instanceof AccessResultInterface && !$data->access
->isAllowed()) {
continue;
}
$element = [];
// Set a variable for the <li> tag. Only set 'expanded' to true if the
// link also has visible children within the current tree.
$element['is_expanded'] = FALSE;
$element['is_collapsed'] = FALSE;
if ($data->hasChildren && !empty($data->subtree)) {
$element['is_expanded'] = TRUE;
}
elseif ($data->hasChildren) {
$element['is_collapsed'] = TRUE;
}
// Set a helper variable to indicate whether the link is in the active
// trail.
$element['in_active_trail'] = FALSE;
if ($data->inActiveTrail) {
$element['in_active_trail'] = TRUE;
}
// Note: links are rendered in the menu.html.twig template; and they
// automatically bubble their associated cacheability metadata.
$element['attributes'] = new Attribute();
$element['title'] = $link
->getTitle();
$element['url'] = $link
->getUrlObject();
$element['url']
->setOption('set_active_class', TRUE);
$element['below'] = $data->subtree ? $this
->buildItems($data->subtree, $tree_access_cacheability, $tree_link_cacheability) : [];
if (isset($data->options)) {
$element['url']
->setOptions(NestedArray::mergeDeep($element['url']
->getOptions(), $data->options));
}
if ($element['url']
->toString() == $current_path) {
$element['url']
->setOption('attributes', array(
'class' => array(
'is-active',
),
));
}
$element['original_link'] = $link;
$items[$link
->getPluginId()] = $element;
}
return $items;
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
MenuLinkTree:: |
protected | property | The controller resolver. | |
MenuLinkTree:: |
protected | property | The active menu trail service. | |
MenuLinkTree:: |
protected | property | The menu link plugin manager. | |
MenuLinkTree:: |
protected | property | The route provider to load routes by name. | |
MenuLinkTree:: |
protected | property | The menu link tree storage. | |
MenuLinkTree:: |
protected | function | Returns a tree containing of MenuLinkTreeElement based upon tree data. | |
MenuLinkTree:: |
public | function |
Gets the link tree parameters for rendering a specific menu. Overrides MenuLinkTreeInterface:: |
|
MenuLinkTree:: |
public | function |
Finds expanded links in a menu given a set of possible parents. Overrides MenuLinkTreeInterface:: |
|
MenuLinkTree:: |
public | function |
Finds the height of a subtree rooted by of the given ID. Overrides MenuLinkTreeInterface:: |
|
MenuLinkTree:: |
public | function |
Loads a menu tree with a menu link plugin instance at each element. Overrides MenuLinkTreeInterface:: |
|
MenuLinkTree:: |
public | function |
Returns the maximum depth of tree that is supported. Overrides MenuLinkTreeInterface:: |
|
MenuLinkTree:: |
public | function |
Applies menu link tree manipulators to transform the given tree. Overrides MenuLinkTreeInterface:: |
|
MenuLinkTree:: |
public | function | Constructs a \Drupal\Core\Menu\MenuLinkTree object. | |
MenuLinkTreeMenuToken:: |
public | function |
Builds a renderable array from a menu tree. Overrides MenuLinkTree:: |
|
MenuLinkTreeMenuToken:: |
protected | function |
Builds the #items property for a menu tree's renderable array. Overrides MenuLinkTree:: |