Same name and namespace in other branches
  1. 7.x includes/menu.inc \menu_local_tasks()
  2. 8.9.x core/includes/menu.inc \menu_local_tasks()

Collects the local tasks (tabs) for a given level.

Parameters

$level: The level of tasks you ask for. Primary tasks are 0, secondary are 1.

$return_root: Whether to return the root path for the current page.

Return value

Themed output corresponding to the tabs of the requested level, or router path if $return_root == TRUE. This router path corresponds to a parent tab, if the current page is a default local task.

Related topics

3 calls to menu_local_tasks()
menu_primary_local_tasks in includes/menu.inc
Returns the rendered local tasks at the top level.
menu_secondary_local_tasks in includes/menu.inc
Returns the rendered local tasks at the second level.
menu_tab_root_path in includes/menu.inc
Returns the router path, or the path of the parent tab of a default local task.

File

includes/menu.inc, line 1315
API for the Drupal menu system.

Code

function 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'];

    // 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.
            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);
            $tabs_current .= theme('menu_local_task', $link, TRUE);
            $next_path = $item['path'];
          }
          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'] : '';
  }
}