Remove unwanted tabs from pages

Description

Some modules add tabs to pages that are not needed for general users, or not needed at all. You may wish to link to the page in a different way, as some users don't understand that they can click on the tab above a node.

There is currently no way to alter the hook_menu() generated tabs from code, so this function will find and strip out a tab based on its name.

Step 1 of 2

Locate your theme's template.php file. If one doesn't exist create an empty one. This is where you can place customisation PHP code.

Step 2 of 2

Custom functions placed in the themes template.php file should begin with the theme name. In the code snippet below replace "yourthemename" with the actual name of your theme, such as "bluemarine".

You may already have a '_phptemplate_variables' function defined depending on what theme you are using, if so do not include the function again from the snippet below.

<?php
function _phptemplate_variables($hook, $vars = array()) {

  if(
$hook == 'page') {
   
yourthemename_removetab('address book', $vars);
   
// add additional lines here to remove other tabs
 
}

  return
$vars;
}

function
yourthemename_removetab($label, &$vars) {
 
$tabs = explode("\n", $vars['tabs']);
 
$vars['tabs'] = '';

  foreach(
$tabs as $tab) {
    if(
strpos($tab, '>' . $label . '<') === FALSE) {
     
$vars['tabs'] .= $tab . "\n";
    }
  }
}
?>

The tab removal work is done in the yourthemename_removetab() function, pass in a plain text tab label, along with the PHPTemplate variables, and the function will remove the tab.

In the above example snippet the 'address book' tab added by the eCommerce package is removed from the users profile page.

Notes

  • Call yourthemename_removetab('tab name', $vars); for each tab you wish to remove.
  • No other modules need to be installed to use this.

works still for Drupal 5

coltrane - January 16, 2008 - 17:09

This method works in Drupal 5 as well.

A slightly cleaner way

alex.k@drupal.org - February 11, 2008 - 15:26

This method works for me and is cleaner (IMO) and more performant than the regexp method:
Tabs are the default way of rendering menu items of type MENU_LOCAL_TASK or MENU_DEFAULT_LOCAL_TASK. So, there is a theming function theme_menu_local_task() in menu.inc that renders them. If you override it in your theme, you can skip certain tabs by simply returning the empty string as their rendered html:
This function would go into template.php in your theme.

<?php
function phptemplate_menu_local_task($mid, $active, $primary) {
 
//Check which tab is being rendered
 
$item = menu_get_item($mid);
 
//Remove core search tab
 
if ($item['path'] == 'search/node') {
    return
'';
  }
 
//The rest is copied from theme_menu_local_task()
 
if ($active) {
    return
'<li class="active">'. menu_item_link($mid) ."</li>\n";
  }
  else {
    return
'<li>'. menu_item_link($mid) ."</li>\n";
  }
}
?>

This example removes the core search tab.

This does not work with some paths

stevekerouac - February 13, 2008 - 13:27

For example,

if ($item['path'] == 'user') {
    return '';
  }

does not remove the Log In tab.

user/login

alex.k@drupal.org - February 18, 2008 - 16:08

The path to the login tab is 'user/login' for me (4.7). If you use that it should work. It's the menu item's path, not the path of the request that should be checked.

Yes, thanks

stevekerouac - April 2, 2008 - 09:13

That did it in 5.7 too.

Doesn't work on 'Content' tab of Search page

gbhatnag - March 25, 2008 - 21:58

For some reason, if I try and remove the 'Content' tab on the Search page, the styling of the tabs gets messed up (there are no more tabs, just a real plain looking bulleted list!). I haven't investigated the code much yet, but looks like there is something special about the first tab in a series of tabs (other tabs that aren't first seem to get cut just fine).

 
 

Drupal is a registered trademark of Dries Buytaert.