You are here

protected function BookManager::doBookTreeBuild in Drupal 9

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

Builds a book tree.

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. _menu_tree_check_access() needs to be invoked afterwards.

Parameters

int $bid: The book ID to find links for.

array $parameters: (optional) An associative array of build parameters. Possible keys:

  • expanded: An array of parent link IDs to return only book links that are children of one of the parent link IDs in this list. If empty, the whole outline is built, unless 'only_active_trail' is TRUE.
  • active_trail: An array of node IDs, representing the currently active book link.
  • only_active_trail: Whether to only return links that are in the active trail. This option is ignored if 'expanded' is non-empty.
  • min_depth: The minimum depth of book links in the resulting tree. Defaults to 1, which is to build the whole tree for the book.
  • max_depth: The maximum depth of book links in the resulting tree.
  • conditions: An associative array of custom database select query condition key/value pairs; see \Drupal\book\BookOutlineStorage::getBookMenuTree() for the actual query.

Return value

array An array with links representing the tree structure of the book.

See also

\Drupal\book\BookOutlineStorageInterface::getBookMenuTree()

1 call to BookManager::doBookTreeBuild()
BookManager::bookTreeBuild in core/modules/book/src/BookManager.php
Builds a book tree, translates links, and checks access.

File

core/modules/book/src/BookManager.php, line 735

Class

BookManager
Defines a book manager.

Namespace

Drupal\book

Code

protected function doBookTreeBuild($bid, array $parameters = []) {

  // Build the cache id; sort parents to prevent duplicate storage and remove
  // default parameter values.
  if (isset($parameters['expanded'])) {
    sort($parameters['expanded']);
  }
  $langcode = $this->languageManager
    ->getCurrentLanguage(LanguageInterface::TYPE_CONTENT)
    ->getId();
  $cid = implode(':', [
    'book-links',
    $bid,
    'tree-data',
    $langcode,
    hash('sha256', serialize($parameters)),
  ]);

  // Get it from cache, if available.
  if ($cache = $this->backendChainedCache
    ->get($cid)) {
    return $cache->data;
  }
  $min_depth = isset($parameters['min_depth']) ? $parameters['min_depth'] : 1;
  $result = $this->bookOutlineStorage
    ->getBookMenuTree($bid, $parameters, $min_depth, static::BOOK_MAX_DEPTH);

  // Build an ordered array of links using the query result object.
  $links = [];
  foreach ($result as $link) {
    $link = (array) $link;
    $links[$link['nid']] = $link;
  }
  $active_trail = isset($parameters['active_trail']) ? $parameters['active_trail'] : [];
  $data['tree'] = $this
    ->buildBookOutlineData($links, $active_trail, $min_depth);
  $data['node_links'] = [];
  $this
    ->bookTreeCollectNodeLinks($data['tree'], $data['node_links']);

  // Cache tree data.
  $this->backendChainedCache
    ->set($cid, $data, Cache::PERMANENT, [
    'bid:' . $bid,
  ]);
  return $data;
}