class NavigationMenuLinkTree

Same name and namespace in other branches
  1. 10 core/modules/navigation/src/Menu/NavigationMenuLinkTree.php \Drupal\navigation\Menu\NavigationMenuLinkTree

Extends MenuLinkTree to add specific theme suggestions for the navigation.

@internal

Hierarchy

Expanded class hierarchy of NavigationMenuLinkTree

1 file declares its use of NavigationMenuLinkTree
NavigationMenuLinkTreeTest.php in core/modules/navigation/tests/src/Kernel/NavigationMenuLinkTreeTest.php
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 22

Namespace

Drupal\navigation\Menu
View source
final class NavigationMenuLinkTree extends MenuLinkTree {
  
  /**
   * Constructs a \Drupal\navigation\Menu\NavigationMenuLinkTree object.
   *
   * @param \Drupal\Core\Menu\MenuTreeStorageInterface $treeStorage
   *   The menu link tree storage.
   * @param \Drupal\Core\Menu\MenuLinkManagerInterface $menuLinkManager
   *   The menu link plugin manager.
   * @param \Drupal\Core\Routing\RouteProviderInterface $routeProvider
   *   The route provider to load routes by name.
   * @param \Drupal\Core\Menu\MenuActiveTrailInterface $menuActiveTrail
   *   The active menu trail service.
   * @param \Drupal\Core\Utility\CallableResolver $callableResolver
   *   The callable resolver.
   * @param \Drupal\Core\Extension\ModuleHandlerInterface $moduleHandler
   *   The module handler.
   */
  public function __construct(MenuTreeStorageInterface $treeStorage, MenuLinkManagerInterface $menuLinkManager, RouteProviderInterface $routeProvider, MenuActiveTrailInterface $menuActiveTrail, CallableResolver $callableResolver, protected ModuleHandlerInterface $moduleHandler) {
    parent::__construct($treeStorage, $menuLinkManager, $routeProvider, $menuActiveTrail, $callableResolver);
  }
  
  /**
   * {@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 = Html::getClass(str_replace('.', '_', $plugin_id));
        $build['#items'][$plugin_id]['class'] = $plugin_class;
        $url = $build['#items'][$plugin_id]['url'];
        $icon_defaults = [
          'pack_id' => 'navigation',
          'icon_id' => $plugin_class,
          'settings' => [
            'class' => 'toolbar-button__icon',
            'size' => 20,
          ],
        ];
        $build['#items'][$plugin_id]['icon'] = NestedArray::mergeDeep($icon_defaults, $url->getOption('icon') ?? []);
      }
    }
    return $build;
  }
  
  /**
   * {@inheritdoc}
   */
  public function transform(array $tree, array $manipulators) {
    $tree = parent::transform($tree, $manipulators);
    $this->moduleHandler
      ->alter('navigation_menu_link_tree', $tree);
    return $tree;
  }

}

Members

Title Sort descending Modifiers Object type Summary Overriden Title
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 Gets the link tree parameters for rendering a specific menu. Overrides MenuLinkTreeInterface::getCurrentRouteMenuTreeParameters
MenuLinkTree::getExpanded public function Finds expanded links in a menu given a set of possible parents. Overrides MenuLinkTreeInterface::getExpanded
MenuLinkTree::getSubtreeHeight public function Finds the height of a subtree rooted by of the given ID. Overrides MenuLinkTreeInterface::getSubtreeHeight
MenuLinkTree::load public function Loads a menu tree with a menu link plugin instance at each element. Overrides MenuLinkTreeInterface::load
MenuLinkTree::maxDepth public function Returns the maximum depth of tree that is supported. Overrides MenuLinkTreeInterface::maxDepth
NavigationMenuLinkTree::build public function Builds a renderable array from a menu tree. Overrides MenuLinkTree::build
NavigationMenuLinkTree::transform public function Applies menu link tree manipulators to transform the given tree. Overrides MenuLinkTree::transform
NavigationMenuLinkTree::__construct public function Constructs a \Drupal\navigation\Menu\NavigationMenuLinkTree object. Overrides MenuLinkTree::__construct

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