You are here

function simplemenu_tree_all_data in SimpleMenu 6

Same name and namespace in other branches
  1. 6.2 simplemenu.module \simplemenu_tree_all_data()
  2. 7 simplemenu.module \simplemenu_tree_all_data()

Modified menu_tree_all_data(), providing the complete menu tree below $root_menu (which can be *any* menu item, not just the root of a custom menu).

@todo we don't actually need $menu_name, $mlid would be sufficient

Parameters

$root_menu: root menu item of the tree to return as "menu_name:mlid" (mlid = menu link id)

1 call to simplemenu_tree_all_data()
simplemenu_menu_tree in ./simplemenu.module
Custom implementation of menu_tree(). We want to retrieve the entire menu structure for a given menu, regardless of whether or not the menu item is expanded or not.

File

./simplemenu.module, line 548
Creates a simplemenu.

Code

function simplemenu_tree_all_data($root_menu = 'navigation:0') {
  static $tree = array();
  list($menu_name, $mlid) = explode(':', $root_menu);

  // Generate the cache ID.
  // "links:navigation:all:2" means "all from root to 2" (what the ...), so for "all from 2 down" we do "links:navigation:all:2:all"
  $cid = "links:{$menu_name}:all:{$mlid}" . ($mlid ? ':all' : '');
  if (!isset($tree[$cid])) {

    // If the static variable doesn't have the data, check {cache_menu}.
    $cache = cache_get($cid, 'cache_menu');
    if ($cache && isset($cache->data)) {
      $data = $cache->data;
    }
    else {

      // Build and run the query, and build the tree.
      $where = '';
      $args = array(
        $menu_name,
      );
      if ($mlid > 0) {
        $item = menu_link_load($mlid);
        if ($item) {

          // The tree is a subtree of $menu_name, so we need to restrict the query to
          // this subtree.
          $px = "p" . (int) $item['depth'];
          $where = " AND ml.{$px} = %d AND ml.mlid != %d";
          $args = array(
            $menu_name,
            $item[$px],
            $mlid,
          );
        }
      }

      // Select the links from the table, and recursively build the tree.  We
      // LEFT JOIN since there is no match in {menu_router} for an external
      // link.
      $data['tree'] = menu_tree_data(db_query("\n        SELECT 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, m.description, ml.*\n        FROM {menu_links} ml LEFT JOIN {menu_router} m ON m.path = ml.router_path\n        WHERE ml.menu_name = '%s'" . $where . "\n        ORDER BY p1 ASC, p2 ASC, p3 ASC, p4 ASC, p5 ASC, p6 ASC, p7 ASC, p8 ASC, p9 ASC", $args));
      $data['node_links'] = array();
      menu_tree_collect_node_links($data['tree'], $data['node_links']);

      // Cache the data.
      cache_set($cid, $data, '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];
}