BookManager::doBookTreeBuild

protected BookManager::doBookTreeBuild($bid, array $parameters = array())

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

File

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

Class

BookManager
Defines a book manager.

Namespace

Drupal\book

Code

protected function doBookTreeBuild($bid, array $parameters = array()) {
  // Static cache of already built menu trees.
  $trees = &drupal_static(__METHOD__, array());
  $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 = array();
    foreach ($result as $link) {
      $link = (array) $link;
      $links[$link['nid']] = $link;
    }
    $active_trail = (isset($parameters['active_trail']) ? $parameters['active_trail'] : array());
    $data['tree'] = $this->buildBookOutlineData($links, $active_trail, $min_depth);
    $data['node_links'] = array();
    $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, array('bid:' . $bid));
    $trees[$tree_cid] = $data;
  }

  return $trees[$tree_cid];
}
doc_Drupal
2016-10-29 08:48:10
Comments
Leave a Comment

Please login to continue.