protected MenuLinkTree::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
\Drupal\Core\Menu\MenuLinkTreeElement[] $tree: A data structure representing the tree, as returned from MenuLinkTreeInterface::load().
\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.
\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 value
array The value to use for the #items property of a renderable menu.
Throws
\DomainException
File
- core/lib/Drupal/Core/Menu/MenuLinkTree.php, line 204
Class
- MenuLinkTree
- Implements the loading, transforming and rendering of menu link trees.
Namespace
Drupal\Core\Menu
Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 | protected function buildItems( array $tree , CacheableMetadata & $tree_access_cacheability , CacheableMetadata & $tree_link_cacheability ) { $items = array (); 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)); // 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 ) : array (); if (isset( $data ->options)) { $element [ 'url' ]->setOptions(NestedArray::mergeDeep( $element [ 'url' ]->getOptions(), $data ->options)); } $element [ 'original_link' ] = $link ; // Index using the link's unique ID. $items [ $link ->getPluginId()] = $element ; } return $items ; } |
Please login to continue.