You are here

function simplemenu_tree_all_data in SimpleMenu 7

Same name and namespace in other branches
  1. 6.2 simplemenu.module \simplemenu_tree_all_data()
  2. 6 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)

File

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

Code

function simplemenu_tree_all_data($root_menu = 'management:0') {
  $tree =& drupal_static(__FUNCTION__, array());
  list($menu_name, $mlid) = explode(':', $root_menu);

  // Generate the cache ID for Drupal 7.
  $max_depth = NULL;
  $cid = 'links:' . $menu_name . ':all:' . $mlid . ':' . $GLOBALS['language']->language . ':' . (int) $max_depth;
  if (!isset($tree[$cid])) {
    $cache = cache_get($cid, 'cache_menu');
    if ($cache && isset($cache->data)) {
      $data = $cache->data;
    }
    else {

      // Build the query using a LEFT JOIN since there is no match in
      // {menu_router} for an external link.
      $query = db_select('menu_links', 'ml', array(
        'fetch' => PDO::FETCH_ASSOC,
      ));
      $query
        ->addTag('translatable');
      $query
        ->leftJoin('menu_router', 'm', 'm.path = ml.router_path');
      $query
        ->fields('ml');
      $query
        ->fields('m', array(
        'load_functions',
        'to_arg_functions',
        'access_callback',
        'access_arguments',
        'page_callback',
        'page_arguments',
        'delivery_callback',
        'tab_parent',
        'tab_root',
        'title',
        'title_callback',
        'title_arguments',
        'theme_callback',
        'theme_arguments',
        'type',
        'description',
      ));
      for ($i = 1; $i <= MENU_MAX_DEPTH; $i++) {
        $query
          ->orderBy('p' . $i, 'ASC');
      }
      $query
        ->condition('ml.menu_name', $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'];
          $and = db_and()
            ->condition("ml.{$px}", $item[$px])
            ->condition("ml.mlid", $mlid, '!=');
          $query
            ->condition($and);
        }
      }

      // Build an ordered array of links using the query result object.
      $links = array();
      foreach ($query
        ->execute() as $item) {
        $links[] = $item;
      }
      $data['tree'] = menu_tree_data($links);
      if (count($data['tree']) == 1) {

        // Move the menu items from below to root
        $key = key($data['tree']);
        foreach ($data['tree'][$key]['below'] as $id => $item) {
          $data['tree'][$id] = $item;
          unset($data['tree'][$key]['below'][$id]);
        }
      }
      $data['node_links'] = array();
      menu_tree_collect_node_links($data['tree'], $data['node_links']);
      menu_tree_check_access($data['tree'], $data['node_links']);
      cache_set($cid, $data, 'cache_menu');
    }
    $tree[$cid] = $data['tree'];
  }
  return $tree[$cid];
}