function ToolbarMenuLinkTree::build

Same name in other branches
  1. 8.9.x core/modules/toolbar/src/Menu/ToolbarMenuLinkTree.php \Drupal\toolbar\Menu\ToolbarMenuLinkTree::build()
  2. 10 core/modules/toolbar/src/Menu/ToolbarMenuLinkTree.php \Drupal\toolbar\Menu\ToolbarMenuLinkTree::build()
  3. 11.x core/modules/toolbar/src/Menu/ToolbarMenuLinkTree.php \Drupal\toolbar\Menu\ToolbarMenuLinkTree::build()

Overrides MenuLinkTree::build

File

core/modules/toolbar/src/Menu/ToolbarMenuLinkTree.php, line 15

Class

ToolbarMenuLinkTree
Extends MenuLinkTree to add specific theme suggestions for the toolbar.

Namespace

Drupal\toolbar\Menu

Code

public function build(array $tree, $level = 0) {
    if ($level == 0) {
        if (!$tree) {
            return [];
        }
        $build = parent::build($tree, $level);
        
        /** @var \Drupal\Core\Menu\MenuLinkInterface $link */
        $first_link = reset($tree)->link;
        // Get the menu name of the first link.
        $menu_name = $first_link->getMenuName();
        // Add a more specific theme suggestion to differentiate this rendered
        // menu from others.
        $build['#menu_name'] = $menu_name;
        $build['#theme'] = 'menu__toolbar__' . strtr($menu_name, '-', '_');
        return $build;
    }
    else {
        return parent::build($tree, $level);
    }
}

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