User module menus revisited

Drupal 6: Effective solution to the problem of wanting custom menu items for the user account pages.

Photo of Greg Harvey
Fri, 2008-02-15 00:03By greg

I wrote a little while ago about the problems surrounding modifying the user module's menu items, specifically in Drupal 5.x. Well, I was working with Sean Burlington this week on a short Drupal job in central London when he hit upon a similar issue.

Sean immediately came up with a neat and effective solution to the problem of wanting custom menu items for the user account pages. To my knowledge this is undocumented on Drupal.org or anywhere else for that matter:

He created new menu items using hook_menu in his own module, each with a callback function. The callback functions simply use drupal_goto to redirect the user to the desired page.

Here's a basic example of creating a menu item to the user account page but giving that item the title of "My Settings", as opposed to "My account", the default item's title.

function mymodule_menu($may_cache) {
global $user;
$items = array();

if ($may_cache) {
$items[] = array(
'path' => 'user/my-settings',
'title' => t('My Settings'),
'callback' => 'mymodule_user_settings',
'type' => MENU_NORMAL_ITEM,
'access' => $user->uid,
);
}
return $items;
}

function mymodule_user_settings() {
global $user;
drupal_goto('user/'.$user->uid);
}
?>

And there you have it. A menu item called "My Settings" which takes you to the user account page, without hacking or otherwise messing with the user module's locked "My account" menu item. Nice!

Just another fine example of why pair programming really works. This never occurred to me, but it's a really neat solution. No two developers think the same way. =)

Edit: I've now tested this and have two additional comments. Firstly, I've now declared the $user object as global in hook_menu (you may have done this already for other purposes) and added the 'access' line to hook_menu, which is required to make the item only appear accessible to logged in users. Secondly, don't forget to clear the cache_menu table in your database to see your new menu items if they are set to cache.