You are here

function book_menu_subtree_data in Drupal 6

Same name and namespace in other branches
  1. 7 modules/book/book.module \book_menu_subtree_data()

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.

3 calls to book_menu_subtree_data()
book_export_html in modules/book/book.pages.inc
This function is called by book_export() to generate HTML for export.
book_prev in modules/book/book.module
Fetches the menu link for the previous page of the book.
_book_admin_table in modules/book/book.admin.inc
Build the table portion of the form for the book administration page.

File

modules/book/book.module, line 1043
Allows users to structure the pages of a site in a hierarchy or outline.

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)) {
      $match = array(
        "menu_name = '%s'",
      );
      $args = array(
        $item['menu_name'],
      );
      $i = 1;
      while ($i <= MENU_MAX_DEPTH && $item["p{$i}"]) {
        $match[] = "p{$i} = %d";
        $args[] = $item["p{$i}"];
        $i++;
      }
      $sql = "\n        SELECT b.*, m.load_functions, m.to_arg_functions, m.access_callback, m.access_arguments, m.page_callback, m.page_arguments, m.title, m.title_callback, m.title_arguments, m.type, ml.*\n        FROM {menu_links} ml INNER JOIN {menu_router} m ON m.path = ml.router_path\n        INNER JOIN {book} b ON ml.mlid = b.mlid\n        WHERE " . implode(' AND ', $match) . "\n        ORDER BY p1 ASC, p2 ASC, p3 ASC, p4 ASC, p5 ASC, p6 ASC, p7 ASC, p8 ASC, p9 ASC";
      $data['tree'] = menu_tree_data(db_query($sql, $args), 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];
}