3

Is it possible to get the title of the currently active secondary tab?

I'd like to re-use the the title of this tab later on in the content. I found menu_local_tasks(1) which comes close, but this returns full themed output. I just want the title as I've defined it in hook_menu().

Edit:
My hook_menu structure (trimmed to just the elements in ,not including sibling elements):

$items['events/%node'] = array(
    'title' => 'Events',
    'page callback' => 'page_events',
    'page arguments' => array(1),
    'type' => MENU_NORMAL_ITEM,
);

$items['events/%node/stats'] = array(
  'title' => 'Stats',
  'access callback' => TRUE,
  'page callback' => 'page_stats',
  'page arguments' => array(1),
  'type' => MENU_LOCAL_TASK,
  'weight' => 3,
);

$items['events/%node/stats/year'] = array(
  'title' => 'This Year',
  'access callback' => TRUE,
  'type' => MENU_DEFAULT_LOCAL_TASK,
  'weight' => 0,
);

$items['events/%node/stats/lastyear'] = array(
  'title' => 'Last Year',
  'access callback' => TRUE,
  'page callback' => 'page_stats',
  'page arguments' => array(1),
  'type' => MENU_LOCAL_TASK,
  'weight' => 1,
); 

For example, when at the page events/%node/stats/year, I'd like to get 'This Year'.

0

3 Answers 3

4

menu_get_active_title() should be the function you are looking for.

function menu_get_active_title() {
  $active_trail = menu_get_active_trail();

  foreach (array_reverse($active_trail) as $item) {
    if (!(bool) ($item['type'] & MENU_IS_LOCAL_TASK)) {
      return $item['title'];
    }
  }
}

It should work for menus for which the menu type contains the flag MENU_LOCAL_TASK (which includes also the MENU_DEFAULT_LOCAL_TASK menu type).

The function is also defined in Drupal 7.

Update: I noticed that for menus like /node/%node/view, menu_get_active_title() returns the title for /node/%node. The following code should return the correct title also in those cases, including in the case the title is given by a title callback. I just tested on Drupal 7; I don't actually have Drupal 6 on the test site I use.

$item = end(menu_get_active_trail());

if (!empty($item['title_callback'])) {
  $args = array();
  if (!empty($item['title_arguments'])) {
    foreach ($item['title_arguments'] as $value) {
      if (is_int($value)) {
        // Get the value from the menu item map.
        if (!empty($item['map'])) {
          $args[] = $item['map'][$value];
        }
      }
      else {
        $args[] = $value;
      }
    }
  }

  $title = call_user_func_array($item['title_callback'], $args);
} 
 else {
   $title = $item['title'];
 }
1
  • This doesn't work for me. It returns the top level menu title. Referencing my edit above, this function returns 'Events', when I'm looking for 'This Year'.
    – ack
    Commented Mar 22, 2011 at 5:50
1

Try menu_get_item($path = NULL, $router_item = NULL) http://api.drupal.org/api/drupal/includes--menu.inc/function/menu_get_item/6. You should be able to pass the path value which will result in a returned array containing details of the menu tab entry including the 'title' value.

Use $_GET['q'] to get the current path.

1

You should be able to do this with drupal_get_headers(), and then parsing through the results.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.