function BookManager::buildBookOutlineRecursive

Same name in other branches
  1. 9 core/modules/book/src/BookManager.php \Drupal\book\BookManager::buildBookOutlineRecursive()
  2. 8.9.x core/modules/book/src/BookManager.php \Drupal\book\BookManager::buildBookOutlineRecursive()
  3. 10 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.

Parameters

array $links: A flat array of book links that are part of the book. Each array element is an associative array of information about the book link, containing the fields from the {book} table. This array must be ordered depth-first.

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

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

Return value

array Book tree.

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 1114

Class

BookManager
Defines a book manager.

Namespace

Drupal\book

Code

protected function buildBookOutlineRecursive(&$links, $parents, $depth) {
    $tree = [];
    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']] = [
            'link' => $item,
            'below' => [],
        ];
        // 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;
}

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