Thursday, January 22, 2009

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.

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.

No comments: