protected function BookManager::doBookTreeBuild in Drupal 8
Same name and namespace in other branches
- 9 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 673
Class
- BookManager
- Defines a book manager.
Namespace
Drupal\bookCode
protected function doBookTreeBuild($bid, array $parameters = []) {
// Static cache of already built menu trees.
$trees =& drupal_static(__METHOD__, []);
$language_interface = \Drupal::languageManager()
->getCurrentLanguage();
// Build the cache id; sort parents to prevent duplicate storage and remove
// default parameter values.
if (isset($parameters['expanded'])) {
sort($parameters['expanded']);
}
$tree_cid = 'book-links:' . $bid . ':tree-data:' . $language_interface
->getId() . ':' . hash('sha256', serialize($parameters));
// If we do not have this tree in the static cache, check {cache_data}.
if (!isset($trees[$tree_cid])) {
$cache = \Drupal::cache('data')
->get($tree_cid);
if ($cache && $cache->data) {
$trees[$tree_cid] = $cache->data;
}
}
if (!isset($trees[$tree_cid])) {
$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 the data, if it is not already in the cache.
\Drupal::cache('data')
->set($tree_cid, $data, Cache::PERMANENT, [
'bid:' . $bid,
]);
$trees[$tree_cid] = $data;
}
return $trees[$tree_cid];
}