7

I have the following function:

function cvminterviews_menu() {

  $items = array();

  $items['admin/settings/cvminterviews'] = array(
    'title' => 'Interview Schedule Overview',
    'description' => 'Overview',
    'file' => 'cvminterviews_admin_overview.inc.php',
    'page callback' => 'drupal_get_form',
    'page arguments' => array('cvminterviews_overview_form'),
    'access arguments' => array('administer cvminterviews'),
  );

  $items['admin/settings/cvminterviews/overview'] = array(
    'title' => 'Overview',
    'description' => 'Overview',
    'file' => 'cvminterviews_admin_overview.inc.php',
    'page callback' => 'drupal_get_form',
    'page arguments' => array('cvminterviews_overview_form'),
    'access arguments' => array('administer cvminterviews'),
    'type' => MENU_DEFAULT_LOCAL_TASK,
  );

  $items['admin/settings/cvminterviews/setup'] = array(
    'title' => 'Interview Schedule Setup',
    'description' => 'Setup',
    'file' => 'cvminterviews_admin_setup.inc.php',
    'page callback' => 'drupal_get_form',
    'page arguments' => array('cvminterviews_dateroomsetup_form'),
    'access arguments' => array('administer cvminterviews'),
    'type' => MENU_LOCAL_TASK,
  );


  return $items;
}

This outputs two tabs at the top of the page. However, when I click on each tab, it has the same page title as the parent menu item "Interview Schedule Overview".

Is there a way to set unique page titles for each tab?

3 Answers 3

2

You have to call

> 4.6 – 6   drupal_set_title($title = NULL)
> 7 – 8     drupal_set_title($title = NULL, $output = CHECK_PLAIN)

You'll have to change your code around a little

  $items['admin/settings/cvminterviews/setup'] = array(
    'title' => 'Interview Schedule Setup',
    'description' => 'Setup',
    'file' => 'cvminterviews_admin_setup.inc.php',
    'page callback' => 'drupal_get_form',
    'page arguments' => array('cvminterviews_dateroomsetup_form'),
    'access arguments' => array('administer cvminterviews'),
    'type' => MENU_LOCAL_TASK,
  );

to

  $items['admin/settings/cvminterviews/setup'] = array(
    'title' => 'Interview Schedule Setup',
    'description' => 'Setup',
    'file' => 'cvminterviews_admin_setup.inc.php',
    'page callback' => '_cvminterviews_admin_page',
    'page arguments' => array(
      'variables' => array(
        'form' => 'cvminterviews_dateroomsetup_form', 
        'title' => t('my_title')
      )
    ),
    'access arguments' => array('administer cvminterviews'),
    'type' => MENU_LOCAL_TASK,
  );

function _cvminterviews_admin_page($variables = NULL) {
  if(isset($variables['title'])) {
    drupal_set_title($variables['title']);
  }
  if(isset($variables['form'])) {
    return drupal_get_form($variables['form']);
  }
}

Then do the same thing for $items[admin/settings/cvminterviews'] & $items['admin/settings/cvminterviews/setup']

When you are changing your code, there is one more problem that you will need to aware of.

There will be 2 tabs on admin/settings/cvminterviews, one that will be active 'Overview' and 'Interview Schedule Setup'. 'Overview' will link to admin/settings/cvminteviews NOT admin/settings/cvminterviews/overview. admin/settings/cvminterviews/overview will never be called (unless you type that it into the address bar) because it is the default tab set by 'type' => MENU_DEFAULT_LOCAL_TASK.

2
  • 1
    I think it's preferred to pass the string literal to t() rather than a variable. You've also forgotten quotes around the keys form and title.
    – Andy
    Jul 18, 2011 at 16:50
  • In case someone had a similar issue, but using entity_ui_get_form as the page callback, check $form['#after_build'], for example: drupalcode.org/project/crm_core.git/blob/…
    – Favio
    Sep 11, 2013 at 6:04
1

I used this code (in my function that set the breadcrumb but this could be used in a page callback too)

<?php
// get current menu item informations. This contain the title of the tab.
$item = menu_get_item();
// use it as page title.
drupal_set_title($item['title']);
?>
-1

I believe in general you tend to keep the same title when using local tasks - the tasks themselves are meant to be actions you can perform on some common object/area, so in that case it does kinda make sense to keep the same title whichever local task you're on.

5
  • In this situation, the return value of 'title callback' changes the title of the tab, not the title of the page. You can add drupal_set_title('my_new_title') inside the callback function and it works.
    – iStryker
    Jul 18, 2011 at 17:11
  • Any feedback on the downvoting?!
    – Andy
    Jul 18, 2011 at 17:11
  • @iStryker - thanks for the feedback. My understanding is that the title is passed as an argument to the title callback, so you can either a) use a title callback of drupal_set_title (which would bypass t()) or b) use a custom callback that passes the title through t() and calls drupal_set_title() manually. Do you see an issue with that approach?
    – Andy
    Jul 18, 2011 at 17:18
  • In my myshift demo. I've added 'title callback' => 'example_title_callback' for all 3 menu items. On all 3 pages the function example_title_callback gets called 2+ times, This is because the menu items in hook_menu() are getting call for every rendered (tab) link residing on the page. (and example_title_callback is getting call everytime to try to determine the title of the link). Therefore if you have drupal_set_title in example_title_callback it gets called 2+ times. This is bad. If you put drupal_set_title inside the page callback, then it only gets called once.
    – iStryker
    Jul 18, 2011 at 17:55
  • 1
    @iStryker Thanks, that's a very good point - as well as being called more than once, the method is inherently broken, as the last tab to have its title run through the callback would be the one to set the page's title (even if it's not the currently active tab).
    – Andy
    Jul 18, 2011 at 18:20

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.