Remove tabs using hook_menu_alter

If you are using Drupal 6 or higher, a new hook is available called hook_menu_alter() which allows you to alter various properties of menu items that are set by core or other modules.

To use this function, you will need to make an extremely simple custom module to include the code snippet in, which the lesson How to create a simple Drupal 6 module in three easy steps should help with (or if you already have a custom module, simply add the function, adjust the name prefix, and clear the menu cache).

Do not include the opening and closing PHP tags, which are used here only to trigger code highlighting:

<?php
/**
* Implementation of hook_menu_alter().
* Remember to clear the menu cache after adding/editing this function.
*/
function MODULENAME_menu_alter(&$items) {
 
// Removing certain local navigation tabs that are either undesired or need to be custom relocated.

  // Set these tabs to MENU_CALLBACK, so they still register the path, but just don't show the tab:
 
$items['node/%node/track']['type'] = MENU_CALLBACK;
 
$items['user/%user/track']['type'] = MENU_CALLBACK;
 
$items['search/user']['type'] = MENU_CALLBACK;
 
 
// Fully unset these tabs and their paths, don't want them at all. This breaks the path as well:
 
unset($items['user/%user/example']);
}
?>

After saving the code snippet in a custom module, clear the menu cache for it to take effect. This can be done by clearing the cache at Administer > Site configuration > Performance, or through helper modules such as Admin Menu (top left icon) or Devel (in the Devel block).

Note that using unset() on a menu item completely removes the path connected with that menu item (for instance, if the menu item being unset had a path of /user/example then that path would now provide a page not found error). Using the unset() method should only be used to completely remove menu items and their paths from a site.

This method should be able to remove most menu tabs, however it cannot remove the View tab. If you need to remove the View tab, you can do so using this alternate technique.

Possible to allow admin to view tabs?

ressa - November 4, 2009 - 13:29

This function works perfectly, but is it possible to hide some tabs for anonymous and registered users, while letting admin view everything and access their paths?

Thanks.

Hide user tabs, except for admin

ressa - November 12, 2009 - 17:47

With a lot of help (thanks!), I finally got it to work.

This function will hide the view, edit, and track user tabs, whereas admin, or user roles that are granted access, can see them. Follow the directions above to implement it in a module:

<?php
// $Id: remove_tabs.module

/**
* Set up a new permission for the module.
* Grant access under admin -> user management -> permissions
*/
function remove_tabs_perm() {
  return array(
'view hidden tabs');
}

/**
* Implementation of hook_menu_alter().
* Remember to clear the menu cache after adding/editing this function.
*/
function remove_tabs_menu_alter(&$items) {
$items['node/%node/track']['access callback'] = 'user_access';
$items['node/%node/track']['access arguments'] = array('view hidden tabs');

$items['user/%user/view']['access callback'] = 'user_access';
$items['user/%user/view']['access arguments'] = array('view hidden tabs');

$items['user/%user/track']['access callback'] = 'user_access';
$items['user/%user/track']['access arguments'] = array('view hidden tabs');

$items['search/user']['access callback'] = 'user_access';
$items['search/user']['access arguments'] = array('view hidden tabs');
}
?>

To remove the personal contact form tab and form, use the x author module

 
 

Drupal is a registered trademark of Dries Buytaert.