You are here

protected function MenuTreeStorage::treeDataRecursive in Drupal 9

Same name and namespace in other branches
  1. 8 core/lib/Drupal/Core/Menu/MenuTreeStorage.php \Drupal\Core\Menu\MenuTreeStorage::treeDataRecursive()

Builds the data representing a menu tree.

The function is a bit complex because the rendering of a link depends on the next menu link.

Parameters

array $links: A flat array of menu links that are part of the menu. Each array element is an associative array of information about the menu link, containing the fields from the $this->table. This array must be ordered depth-first. MenuTreeStorage::loadTreeData() includes a sample query.

array $parents: An array of the menu link ID values that are in the path from the current page to the root of the menu tree.

int $depth: The minimum depth to include in the returned menu tree.

Return value

array The fully built tree.

See also

\Drupal\Core\Menu\MenuTreeStorage::loadTreeData()

1 call to MenuTreeStorage::treeDataRecursive()
MenuTreeStorage::doBuildTreeData in core/lib/Drupal/Core/Menu/MenuTreeStorage.php
Prepares the data for calling $this->treeDataRecursive().

File

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

Class

MenuTreeStorage
Provides a menu tree storage using the database.

Namespace

Drupal\Core\Menu

Code

protected function treeDataRecursive(array &$links, array $parents, $depth) {
  $tree = [];
  while ($tree_link_definition = array_pop($links)) {
    $tree[$tree_link_definition['id']] = [
      'definition' => $this
        ->prepareLink($tree_link_definition, TRUE),
      'has_children' => $tree_link_definition['has_children'],
      // We need to determine if we're on the path to root so we can later
      // build the correct active trail.
      'in_active_trail' => in_array($tree_link_definition['id'], $parents),
      'subtree' => [],
      'depth' => $tree_link_definition['depth'],
    ];

    // Look ahead to the next link, but leave it on the array so it's
    // available to other recursive function calls if we return or build a
    // sub-tree.
    $next = end($links);

    // Check whether the next link is the first in a new sub-tree.
    if ($next && $next['depth'] > $depth) {

      // Recursively call doBuildTreeData to build the sub-tree.
      $tree[$tree_link_definition['id']]['subtree'] = $this
        ->treeDataRecursive($links, $parents, $next['depth']);

      // Fetch next link after filling the sub-tree.
      $next = end($links);
    }

    // Determine if we should exit the loop and return.
    if (!$next || $next['depth'] < $depth) {
      break;
    }
  }
  return $tree;
}