protected function MenuTreeStorage::treeDataRecursive

protected MenuTreeStorage::treeDataRecursive(array &$links, array $parents, $depth)

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()

File

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

Class

MenuTreeStorage
Provides a menu tree storage using the database.

Namespace

Drupal\Core\Menu

Code

protected function treeDataRecursive(array &$links, array $parents, $depth) {
  $tree = array();
  while ($tree_link_definition = array_pop($links)) {
    $tree[$tree_link_definition['id']] = array(
      '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' => array(),
      '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;
}

© 2001–2016 by the original authors
Licensed under the GNU General Public License, version 2 and later.
Drupal is a registered trademark of Dries Buytaert.
https://api.drupal.org/api/drupal/core!lib!Drupal!Core!Menu!MenuTreeStorage.php/function/MenuTreeStorage::treeDataRecursive/8.1.x