You are here

public function UltimenuManager::buildMenuTree in Ultimenu 8.2

Same name and namespace in other branches
  1. 8 src/UltimenuManager.php \Drupal\ultimenu\UltimenuManager::buildMenuTree()

Build the menu to contain Ultimenu regions.

Helper function for ::build().

Parameters

array $config: The config available for the menu tree.

\Drupal\Core\Cache\CacheableMetadata &$tree_access_cacheability: Internal use only. The aggregated cacheability metadata for the access results across the entire tree. Used when rendering the root level.

\Drupal\Core\Cache\CacheableMetadata &$tree_link_cacheability: Internal use only. The aggregated cacheability metadata for the menu links across the entire tree. Used when rendering the root level.

Return value

array The value to use for the #items property of a renderable menu.

Throws

\DomainException

Overrides UltimenuManagerInterface::buildMenuTree

1 call to UltimenuManager::buildMenuTree()
UltimenuManager::preRenderBuild in src/UltimenuManager.php
Builds the Ultimenu outputs as a structured array ready for ::renderer().

File

src/UltimenuManager.php, line 277

Class

UltimenuManager
Implements UltimenuManagerInterface.

Namespace

Drupal\ultimenu

Code

public function buildMenuTree(array $config, CacheableMetadata &$tree_access_cacheability, CacheableMetadata &$tree_link_cacheability) {
  $menu_name = $config['menu_name'];
  $active_trails = $this->tree
    ->getMenuActiveTrail()
    ->getActiveTrailIds($menu_name);
  $tree = $this->tree
    ->loadMenuTree($menu_name);
  if (empty($tree)) {
    return [];
  }
  $ultimenu = [];
  foreach ($tree as $data) {

    // Generally we only deal with visible links, but just in case.
    if (!$data->link
      ->isEnabled()) {
      continue;
    }
    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;
    }
    $ultimenu[$data->link
      ->getPluginId()] = $this
      ->buildMenuItem($data, $active_trails, $config);
  }
  return $ultimenu;
}