class NavigationMenuLinkTree

Same name in other branches
  1. 11.x core/modules/navigation/src/Menu/NavigationMenuLinkTree.php \Drupal\navigation\Menu\NavigationMenuLinkTree

Extends MenuLinkTree to add specific theme suggestions for the navigation.

@internal

Hierarchy

  • class \Drupal\Core\Menu\MenuLinkTree implements \Drupal\Core\Menu\MenuLinkTreeInterface

Expanded class hierarchy of NavigationMenuLinkTree

1 string reference to 'NavigationMenuLinkTree'
navigation.services.yml in core/modules/navigation/navigation.services.yml
core/modules/navigation/navigation.services.yml
1 service uses NavigationMenuLinkTree
navigation.menu_tree in core/modules/navigation/navigation.services.yml
Drupal\navigation\Menu\NavigationMenuLinkTree

File

core/modules/navigation/src/Menu/NavigationMenuLinkTree.php, line 14

Namespace

Drupal\navigation\Menu
View source
final class NavigationMenuLinkTree extends MenuLinkTree {
    
    /**
     * {@inheritdoc}
     */
    public function build(array $tree) : array {
        if (!$tree) {
            return [];
        }
        $build = parent::build($tree);
        if (empty($build['#items'])) {
            return [];
        }
        
        /** @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'] = 'navigation_menu__' . strtr($menu_name, '-', '_');
        // Loop through menu items and add the plugin id as a class.
        foreach ($tree as $item) {
            if ($item->access
                ->isAllowed()) {
                $plugin_id = $item->link
                    ->getPluginId();
                $plugin_class = str_replace('.', '_', $plugin_id);
                $build['#items'][$plugin_id]['class'] = $plugin_class;
            }
        }
        return $build;
    }

}

Members

Title Sort descending Modifiers Object type Summary Overriden Title
MenuLinkTree::$callableResolver protected property The callable resolver.
MenuLinkTree::$menuActiveTrail protected property The active menu trail service.
MenuLinkTree::$menuLinkManager protected property The menu link plugin manager.
MenuLinkTree::$routeProvider protected property The route provider to load routes by name.
MenuLinkTree::$treeStorage protected property The menu link tree storage.
MenuLinkTree::buildItems protected function Builds the #items property for a menu tree's renderable array.
MenuLinkTree::createInstances protected function Returns a tree containing of MenuLinkTreeElement based upon tree data.
MenuLinkTree::getCurrentRouteMenuTreeParameters public function
MenuLinkTree::getExpanded public function
MenuLinkTree::getSubtreeHeight public function
MenuLinkTree::load public function
MenuLinkTree::maxDepth public function
MenuLinkTree::transform public function
MenuLinkTree::__construct public function Constructs a \Drupal\Core\Menu\MenuLinkTree object.
NavigationMenuLinkTree::build public function Overrides MenuLinkTree::build

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