Drupal Alters and Overrides: hook_menu_alter

Tags: 

This brief tutorial will cover altering and removing items in Drupal's menu system. See my previous blog post on hook_form_alter.

hook_menu_alter

Introduced in Drupal 6, hook_menu_alter allows you to actually alter elements in Drupal's menu system. The primary links, secondary links, tabs and page callbacks are alterable in this way. For example-- using the Content Profile module, it adds an extra tab to each user's page. If I choose the option to "Show a tab at the user's page", it will then display an extra tab when visiting that profile.

The problem with this is I find the tabs a bit unclear and want to rename them; instead of "View", "Edit" and "Profile", I want them to say "View", "Edit Account Settings", and "Edit Personal Information".

In my demo module, I'll add the following:

/**
 * Implementation of hook_menu_alter().
 */
function demo_menu_alter(&$items) {
  // Change the name of user profile tabs. 
  $items['user/%user_category/edit']['title'] = 'Edit Account Settings';
  $items['user/%user/profile/profile']['title'] = 'Edit Personal Information';
}

You can, of course, also remove menu items or tabs this way. You can find and target your specific menu item with a print_r($items) or dpm($items).

/**
 * Implementation of hook_menu_alter().
 */
function demo_menu_alter(&$items) {
  // Change the name of user profile tabs. 
  $items['user/%user/profile/profile']['title'] = 'Edit Personal Information';
  unset($items['user/%user_category/edit']['title']);
}

In Drupal 6, you can also use the Tab Tamer module.

2 Comments

Add new comment