6

I'm building a menu with hook_menu().

When you're building the array for this you specify a title, which is then passed to the page you call and becomes the page title. It's also the menu item's displayed name.

$items['somewhere/something'] = array(
 'title' => 'Something',
 'description' => 'something else',
);

This would generate a menu link called "Something", and the page title would be the same. Is there a way to make the menu item's name one thing, but pass along a different title for the page?

I know I can use drupal_set_title() in the actual hook_page() function, but I'd have to perform an additional database pull to make that happen in this case, and I'd rather not do that.

  • What a simple yet interesting question. A quick look into this shows that there may not be an easy (from the hook_menu) way to do this! see this issue (thats currently for Drupal 8!) drupal.org/node/465958 – electblake Apr 6 '11 at 19:19
  • Well I'm still in Drupal 6 so that won't help unfortunately :( – oranges13 Apr 6 '11 at 19:52
  • How would using drupal_set_title() cause an extra database query? – tim.plunkett Apr 6 '11 at 20:02
  • 1
    Also you can use phptemplate_preprocess_page(&$vars) function to redeclare page title – dobeerman Apr 6 '11 at 20:23
  • @tim.plunkett in the module I'm writing I'm generating the menu from a database (outside of drupal). During this query I have the title I wish to pass, but I don't want that to be the title of the menu item as well. I want the menu item to be one word of this title. In order to change the title using drupal_set_title() in the hook_page() function, I have to query the database again unless I want to pass it through as a url argument, which I don't wish to do. – oranges13 Apr 8 '11 at 12:43
5

No there's no way to do it with hook_menu().

The simplest way is what you suggested, using drupal_set_title() and the most efficient is what dobeerman suggested above: use phptemplate_preprocess_page() and alter $vars accordingly.

Honestly though, I would just use drupal_set_title() even if there is that extra db call you mentioned (unconfirmed). This is a tiny optimization and if performance is important to you, you should have some caching mechanism in place (Varnish, Boost, etc) and this extra call won't make a difference!

  • The description for drupal_set_title() is the following: "Set the title of the current page, for display on the page and in the title bar." – kiamlaluno Apr 6 '11 at 22:06
  • 2
    First, let's drop the phptemplate_ prefix as in drupal.org/node/422116. I'd go for themename_preprocess_page since it caches and doesn't query the database. – Capi Etheriel Apr 7 '11 at 18:39
1

To set a different title for the page (the one used in the <title> tag), you have two possibilities:

  • In your module, implement MODULE_preprocess_page()and use it to alter the value of $variables['head_title']. The documentation page for template_preprocess_page() reports which values are available in the $variables array.
  • Install the Page Title module, which allows you change the title set for a page. It allows you to execute a PHP snippet to set the page title, and it also uses tokens.

Using drupal_set_title(), you don't set just the page title or the title visible in the title bar; you set both, as reported in the documentation.

Set the title of the current page, for display on the page and in the title bar.

Parameters

$title
Optional string value to assign to the page title; or if set to NULL (default), leaves the current title unchanged.

Return value

The updated title of the current page.

  • Despite being correct in everything you said, you didn't answer the question directly! The OP wants to know about having one text as the title in a hook_menu() and another text displayed in the <title> tag on the page, for the same address! – Alex Weber Apr 7 '11 at 1:45
  • @Alex To say it more directly, I should do it in the website he has. Why do you think that saying which hook to implement and which variable needs to be altered, or saying which module needs to be installed is not direct? – kiamlaluno Apr 7 '11 at 10:03
  • @kiamluno fair enough, good point! :) – Alex Weber Apr 7 '11 at 12:29
  • In this use case, the menu was already being generated in a custom module, so writing another one and using MODULE_preprocess_page is silly. Installing the Page Title module in this case would not have worked because I need the page title to be based on the query from an outside data source which is used to create the menu - tokens wouldn't work for that (and the pages are constantly changed and updated - added and removed based on that outside data source). It was a complex problem to be sure! – oranges13 Sep 3 '15 at 13:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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