You are here

class SitemapMenuLinkTree in Sitemap 2.0.x

Same name and namespace in other branches
  1. 8.2 src/Menu/SitemapMenuLinkTree.php \Drupal\sitemap\Menu\SitemapMenuLinkTree

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

Hierarchy

Expanded class hierarchy of SitemapMenuLinkTree

1 string reference to 'SitemapMenuLinkTree'
sitemap.services.yml in ./sitemap.services.yml
sitemap.services.yml
1 service uses SitemapMenuLinkTree
sitemap.menu.link_tree in ./sitemap.services.yml
\Drupal\sitemap\Menu\SitemapMenuLinkTree

File

src/Menu/SitemapMenuLinkTree.php, line 14

Namespace

Drupal\sitemap\Menu
View source
class SitemapMenuLinkTree extends MenuLinkTree {

  /**
   * {@inheritdoc}
   */
  protected function buildItems(array $tree, CacheableMetadata &$tree_access_cacheability, CacheableMetadata &$tree_link_cacheability) {
    $items = [];
    foreach ($tree as $data) {

      /** @var \Drupal\Core\Menu\MenuLinkInterface $link */
      $link = $data->link;

      // Removed $link->isEnabled() check.
      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) : [];
      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;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
MenuLinkTree::$controllerResolver protected property The controller 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::build public function Builds a renderable array from a menu tree. Overrides MenuLinkTreeInterface::build 1
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
MenuLinkTree::transform public function Applies menu link tree manipulators to transform the given tree. Overrides MenuLinkTreeInterface::transform
MenuLinkTree::__construct public function Constructs a \Drupal\Core\Menu\MenuLinkTree object.
SitemapMenuLinkTree::buildItems protected function Builds the #items property for a menu tree's renderable array. Overrides MenuLinkTree::buildItems