You are here

function ctools_menu_local_tasks in Chaos Tool Suite (ctools) 6

CTools' variant of menu_local_tasks.

This function is a new version of menu_local_tasks that is meant to be more flexible and allow for modules to dynamically add items to the local tasks using a simple function.

One downside to using this is that the code to build the tabs could be run twice on a page if something

3 calls to ctools_menu_local_tasks()
ctools_menu_primary_local_tasks in includes/menu.inc
CTools variant of menu_primary_local_tasks().
ctools_menu_secondary_local_tasks in includes/menu.inc
CTools variant of menu_secondary_local_tasks().
ctools_menu_tab_root_path in includes/menu.inc
CTools' replacement for menu_tab_root_path()

File

includes/menu.inc, line 280
General menu helper functions.

Code

function ctools_menu_local_tasks($level = 0, $return_root = FALSE) {
  static $tabs;
  static $root_path;
  if (!isset($tabs)) {
    $tabs = array();
    $router_item = menu_get_item();
    if (!$router_item || !$router_item['access']) {
      return '';
    }

    // Get all tabs and the root page.
    $result = db_query("SELECT * FROM {menu_router} WHERE tab_root = '%s' ORDER BY weight, title", $router_item['tab_root']);
    $map = arg();
    $children = array();
    $tasks = array();
    $root_path = $router_item['path'];
    while ($item = db_fetch_array($result)) {
      _menu_translate($item, $map, TRUE);
      if ($item['tab_parent']) {

        // All tabs, but not the root page.
        $children[$item['tab_parent']][$item['path']] = $item;
      }

      // Store the translated item for later use.
      $tasks[$item['path']] = $item;
    }

    // Find all tabs below the current path.
    $path = $router_item['path'];
    _ctools_menu_add_dynamic_items($children[$path]);

    // Tab parenting may skip levels, so the number of parts in the path may not
    // equal the depth. Thus we use the $depth counter (offset by 1000 for ksort).
    $depth = 1001;
    while (isset($children[$path])) {
      $tabs_current = '';
      $next_path = '';
      $count = 0;
      foreach ($children[$path] as $item) {
        if ($item['access']) {
          $count++;

          // The default task is always active.
          if ($item['type'] == MENU_DEFAULT_LOCAL_TASK) {

            // Find the first parent which is not a default local task.
            if (isset($item['tab_parent'])) {
              for ($p = $item['tab_parent']; $tasks[$p]['type'] == MENU_DEFAULT_LOCAL_TASK; $p = $tasks[$p]['tab_parent']) {
              }
              $href = $tasks[$p]['href'];
              $next_path = $item['path'];
            }
            else {
              $href = $item['href'];
            }
            $link = theme('menu_item_link', array(
              'href' => $href,
            ) + $item);
            $tabs_current .= theme('menu_local_task', $link, TRUE);
          }
          else {
            $link = theme('menu_item_link', $item);
            $tabs_current .= theme('menu_local_task', $link);
          }
        }
      }
      $path = $next_path;
      $tabs[$depth]['count'] = $count;
      $tabs[$depth]['output'] = $tabs_current;
      $depth++;
    }

    // Find all tabs at the same level or above the current one.
    $parent = $router_item['tab_parent'];
    $path = $router_item['path'];
    $current = $router_item;
    $depth = 1000;
    while (isset($children[$parent])) {
      $tabs_current = '';
      $next_path = '';
      $next_parent = '';
      $count = 0;
      foreach ($children[$parent] as $item) {
        if ($item['access']) {
          $count++;
          if ($item['type'] == MENU_DEFAULT_LOCAL_TASK) {

            // Find the first parent which is not a default local task.
            for ($p = $item['tab_parent']; $tasks[$p]['type'] == MENU_DEFAULT_LOCAL_TASK; $p = $tasks[$p]['tab_parent']) {
            }
            $link = theme('menu_item_link', array(
              'href' => $tasks[$p]['href'],
            ) + $item);
            if ($item['path'] == $router_item['path']) {
              $root_path = $tasks[$p]['path'];
            }
          }
          else {
            $link = theme('menu_item_link', $item);
          }

          // We check for the active tab.
          if ($item['path'] == $path) {
            $tabs_current .= theme('menu_local_task', $link, TRUE);
            $next_path = $item['tab_parent'];
            if (isset($tasks[$next_path])) {
              $next_parent = $tasks[$next_path]['tab_parent'];
            }
          }
          else {
            $tabs_current .= theme('menu_local_task', $link);
          }
        }
      }
      $path = $next_path;
      $parent = $next_parent;
      $tabs[$depth]['count'] = $count;
      $tabs[$depth]['output'] = $tabs_current;
      $depth--;
    }

    // Sort by depth.
    ksort($tabs);

    // Remove the depth, we are interested only in their relative placement.
    $tabs = array_values($tabs);
  }
  if ($return_root) {
    return $root_path;
  }
  else {

    // We do not display single tabs.
    return isset($tabs[$level]) && $tabs[$level]['count'] > 1 ? $tabs[$level]['output'] : '';
  }
}