Drupal 7.x hook_menu not displaying page title

Since Drupal 7 was in development for a long time, I, like I imagine many others, started building sites using the various alpha- and beta- releases that have been available since about the summer of 2009. I didn’t want to start a project built on the soon-to-be-superceded 6.x codebase, and 7.x had a number of improvements I was keen to use. Each development release was actually amazingly stable – I’ve been using an alpha6 release in a near-production environment for many months with absolutely no issues whatsoever. Since I was following the development of HEAD relatively closely, I could monitor most changes between versions as they affected me. But then, other projects came up (as they often do), and I lost track of some of the development changes that had happened.

Following the official release of Drupal 7, I then decided to “upgrade” all my sites to use the official version 7.0, and noticed that, right up to the last minute before release, breaking changes were being introduced to the API that I wasn’t aware of. My latest headache was caused by discovering that some of my pages had mysteriously lost their page titles:

image

Shown above is a page from a development site in Drupal 7.x-alpha6

image

And, above, after upgrading my codebase to Drupal 7.0. Notice how the page “Alcohol Quiz” has mysteriously been retitled as just “Home”?

To cut a very long story short, after having checked my code lots of times, my theme files etc., I discovered that there had been a change made to the behaviour of breadcrumbs of MENU_CALLBACK items in http://drupal.org/node/907690, and this has a knock-on effect for page titles. So, under Drupal 6.x and early releases of Drupal 7.x, the following implementation of hook_menu() defines both a url callback and page title (but not a menu item link):


function test_menu() {
 $items = array();
 $items['menupath'] =  array(
 'title' => 'Page Title',
 'type' =>  MENU_CALLBACK,
 …  );

return $items;
}

Now it only registers the url calback. So you can go to the page at /menupath, but there won’t be a title set for that page, and it will therefore default to “Home” as seen in the above screenshots.

The solution if you want to register both a callback and a page title (but still no menu item) is to define your menu item as type MENU_VISIBLE_IN_BREADCRUMB instead:


function test_menu() {
 $items = array();
 $items['menupath'] =  array(
 'title' => 'Page Title',
 'type' =>  MENU_VISIBLE_IN_BREADCRUMB,
 …  );

return $items;
}

(this option does not appear to be documented at http://api.drupal.org/api/drupal/modules–system–system.api.php/function/hook_menu/7)

This entry was posted in Drupal and tagged . Bookmark the permalink.

One Response to Drupal 7.x hook_menu not displaying page title

  1. Sampo says:

    Thanks for sharing!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s