You are here

protected function BookManager::buildBookOutlineRecursive in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 core/modules/book/src/BookManager.php \Drupal\book\BookManager::buildBookOutlineRecursive()

Builds the data representing a book tree.

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

1 call to BookManager::buildBookOutlineRecursive()
BookManager::buildBookOutlineData in core/modules/book/src/BookManager.php
Sorts and returns the built data representing a book tree.

File

core/modules/book/src/BookManager.php, line 1013
Contains \Drupal\book\BookManager.

Class

BookManager
Defines a book manager.

Namespace

Drupal\book

Code

protected function buildBookOutlineRecursive(&$links, $parents, $depth) {
  $tree = array();
  while ($item = array_pop($links)) {

    // We need to determine if we're on the path to root so we can later build
    // the correct active trail.
    $item['in_active_trail'] = in_array($item['nid'], $parents);

    // Add the current link to the tree.
    $tree[$item['nid']] = array(
      'link' => $item,
      'below' => array(),
    );

    // 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 buildBookOutlineRecursive to build the sub-tree.
      $tree[$item['nid']]['below'] = $this
        ->buildBookOutlineRecursive($links, $parents, $next['depth']);

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

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