function MenuLinkTree::build

Same name in other branches
  1. 9 core/lib/Drupal/Core/Menu/MenuLinkTree.php \Drupal\Core\Menu\MenuLinkTree::build()
  2. 8.9.x core/lib/Drupal/Core/Menu/MenuLinkTree.php \Drupal\Core\Menu\MenuLinkTree::build()
  3. 11.x core/lib/Drupal/Core/Menu/MenuLinkTree.php \Drupal\Core\Menu\MenuLinkTree::build()
2 calls to MenuLinkTree::build()
NavigationMenuLinkTree::build in core/modules/navigation/src/Menu/NavigationMenuLinkTree.php
ToolbarMenuLinkTree::build in core/modules/toolbar/src/Menu/ToolbarMenuLinkTree.php
2 methods override MenuLinkTree::build()
NavigationMenuLinkTree::build in core/modules/navigation/src/Menu/NavigationMenuLinkTree.php
ToolbarMenuLinkTree::build in core/modules/toolbar/src/Menu/ToolbarMenuLinkTree.php

File

core/lib/Drupal/Core/Menu/MenuLinkTree.php, line 162

Class

MenuLinkTree
Implements the loading, transforming and rendering of menu link trees.

Namespace

Drupal\Core\Menu

Code

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\Core\Render\Element::children() 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;
    }
    return $build;
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.