book_menu_subtree_data

Definition

book_menu_subtree_data($item)
drupal/modules/book/book.module, line 1164

Description

Get 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 descendents in the menu tree.

Parameters

$item A fully loaded menu link.

Return value

An subtree of menu links in an array, in the order they should be rendered.

Code

function book_menu_subtree_data($item) {
  static $tree = array();

  // Generate a cache ID (cid) specific for this $menu_name and $item.
  $cid = 'links:' . $item['menu_name'] . ':subtree-cid:' . $item['mlid'];

  if (!isset($tree[$cid])) {
    $cache = cache_get($cid, 'cache_menu');

    if ($cache && isset($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 = cache_get($cache->data, 'cache_menu');

      if ($cache && isset($cache->data)) {
        $data = $cache->data;
      }
    }

    // If the subtree data was not in the cache, $data will be NULL.
    if (!isset($data)) {
      $query = db_select('menu_links', 'ml');
      $menu_router_alias = $query->join('menu_router', 'm', 'm.path = ml.router_path');
      $book_alias = $query->join('book', 'b', 'ml.mlid = b.mlid');
      $query->fields($book_alias);
      $query->fields($menu_router_alias, array('load_functions', 'to_arg_functions', 'access_callback', 'access_arguments', 'page_callback', 'page_arguments', 'title', 'title_callback', 'title_arguments', 'type'));
      $query->fields('ml');
      $query->condition('menu_name', $item['menu_name']);
      for ($i = 1; $i <= MENU_MAX_DEPTH && $item["p$i"]; ++$i) {
        $query->condition("p$i", $item["p$i"]);
      }
      for ($i = 1; $i <= MENU_MAX_DEPTH; ++$i) {
        $query->orderBy("p$i");
      }

      $data['tree'] = menu_tree_data($query->execute(), array(), $item['depth']);
      $data['node_links'] = array();
      menu_tree_collect_node_links($data['tree'], $data['node_links']);
      // Compute the real cid for book subtree data.
      $tree_cid = 'links:' . $item['menu_name'] . ':subtree-data:' . md5(serialize($data));
      // Cache the data, if it is not already in the cache.

      if (!cache_get($tree_cid, 'cache_menu')) {
        cache_set($tree_cid, $data, 'cache_menu');
      }
      // Cache the cid of the (shared) data using the menu and item-specific cid.
      cache_set($cid, $tree_cid, 'cache_menu');
    }
    // Check access for the current user to each item in the tree.
    menu_tree_check_access($data['tree'], $data['node_links']);
    $tree[$cid] = $data['tree'];
  }

  return $tree[$cid];
}