function MenuTreeStorage::loadTreeData

Loads a menu link tree from the storage.

This function may be used build the data for a menu tree only, for example to further massage the data manually before further processing happens. MenuLinkTree::checkAccess() needs to be invoked afterwards.

The tree order is maintained using an optimized algorithm, for example by storing each parent in an individual field. However, any details of the storage should not be relied upon since it may be swapped with a different implementation.

Parameters

string $menu_name: The name of the menu.

\Drupal\Core\Menu\MenuTreeParameters $parameters: The parameters to determine which menu links to be loaded into a tree.

Return value

array An array with 2 elements:

  • tree: A fully built menu tree containing an array. @see static::treeDataRecursive()
  • route_names: An array of all route names used in the tree.

Overrides MenuTreeStorageInterface::loadTreeData

2 calls to MenuTreeStorage::loadTreeData()
MenuTreeStorage::loadSubtreeData in core/lib/Drupal/Core/Menu/MenuTreeStorage.php
Loads a subtree rooted by the given ID.
WorkspacesMenuTreeStorage::loadTreeData in core/modules/workspaces/src/WorkspacesMenuTreeStorage.php
Loads a menu link tree from the storage.
1 method overrides MenuTreeStorage::loadTreeData()
WorkspacesMenuTreeStorage::loadTreeData in core/modules/workspaces/src/WorkspacesMenuTreeStorage.php
Loads a menu link tree from the storage.

File

core/lib/Drupal/Core/Menu/MenuTreeStorage.php, line 823

Class

MenuTreeStorage
Provides a menu tree storage using the database.

Namespace

Drupal\Core\Menu

Code

public function loadTreeData($menu_name, MenuTreeParameters $parameters) {
  $tree_cid = "tree-data:{$menu_name}:" . serialize($parameters);
  $cache = $this->menuCacheBackend
    ->get($tree_cid);
  if ($cache && isset($cache->data)) {
    $data = $cache->data;
    // Cache the definitions in memory so they don't need to be loaded again.
    $this->definitions += $data['definitions'];
    unset($data['definitions']);
  }
  else {
    $links = $this->loadLinks($menu_name, $parameters);
    $data['tree'] = $this->doBuildTreeData($links, $parameters->activeTrail, $parameters->minDepth);
    $data['definitions'] = [];
    $data['route_names'] = $this->collectRoutesAndDefinitions($data['tree'], $data['definitions']);
    $this->menuCacheBackend
      ->set($tree_cid, $data, Cache::PERMANENT, [
      'config:system.menu.' . $menu_name,
    ]);
    // The definitions were already added to $this->definitions in
    // $this->doBuildTreeData()
    unset($data['definitions']);
  }
  return $data;
}

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