You are here

public function BookManager::bookSubtreeData in Zircon Profile 8.0

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

Gets the data representing a subtree of the book hierarchy.

The root of the subtree will be the link passed as a parameter, so the returned tree will contain this item and all its descendants in the menu tree.

Parameters

array $link: A fully loaded book link.

Return value

A subtree of book links in an array, in the order they should be rendered.

Overrides BookManagerInterface::bookSubtreeData

File

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

Class

BookManager
Defines a book manager.

Namespace

Drupal\book

Code

public function bookSubtreeData($link) {
  $tree =& drupal_static(__METHOD__, array());

  // Generate a cache ID (cid) specific for this $link.
  $cid = 'book-links:subtree-cid:' . $link['nid'];
  if (!isset($tree[$cid])) {
    $tree_cid_cache = \Drupal::cache('data')
      ->get($cid);
    if ($tree_cid_cache && $tree_cid_cache->data) {

      // If the cache entry exists, it will just be the cid for the actual
      // data. This avoids duplication of large amounts of data.
      $cache = \Drupal::cache('data')
        ->get($tree_cid_cache->data);
      if ($cache && isset($cache->data)) {
        $data = $cache->data;
      }
    }

    // If the subtree data was not in the cache, $data will be NULL.
    if (!isset($data)) {
      $result = $this->bookOutlineStorage
        ->getBookSubtree($link, static::BOOK_MAX_DEPTH);
      $links = array();
      foreach ($result as $item) {
        $links[] = $item;
      }
      $data['tree'] = $this
        ->buildBookOutlineData($links, array(), $link['depth']);
      $data['node_links'] = array();
      $this
        ->bookTreeCollectNodeLinks($data['tree'], $data['node_links']);

      // Compute the real cid for book subtree data.
      $tree_cid = 'book-links:subtree-data:' . hash('sha256', serialize($data));

      // Cache the data, if it is not already in the cache.
      if (!\Drupal::cache('data')
        ->get($tree_cid)) {
        \Drupal::cache('data')
          ->set($tree_cid, $data, Cache::PERMANENT, array(
          'bid:' . $link['bid'],
        ));
      }

      // Cache the cid of the (shared) data using the book and item-specific
      // cid.
      \Drupal::cache('data')
        ->set($cid, $tree_cid, Cache::PERMANENT, array(
        'bid:' . $link['bid'],
      ));
    }

    // Check access for the current user to each item in the tree.
    $this
      ->bookTreeCheckAccess($data['tree'], $data['node_links']);
    $tree[$cid] = $data['tree'];
  }
  return $tree[$cid];
}