Removing and Altering Tab Names In Drupal 5

Altering menu items in drupal 6 is fairly easy thanks to hook_menu_alter. But, in drupal 5 it's not so easy. While we can't achieve all the power of hook_menu_alter we can remove tabs, among other things. Let's take a look at how to do this.

To remove tabs put the following function in your template.php file.

<?php
/**
* Remove a tab
*
* @param  $label
* The label to remove
*
* @param  $vars
* $vars from _phptemplate_variables
*/
function phptemplate_remove_tab($label, &$vars) {
 
$tabs = explode("\n", $vars['tabs']);
 
$vars['tabs'] = '';

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

Then, in the function _phptemplate_variables use this new function like the following example.

<?php
function _phptemplate_variables($hook, $vars = array()) {
  if (
$hook == 'page') {
   
phptemplate_remove_tab('View', &$vars);
  }
}
?>

This would remove all tabs with the name View. If we wanted to get more complicated we can use some arguments. This example only removes the View tab on user pages.
<?php
function _phptemplate_variables($hook, $vars = array()) {
  if (
$hook == 'page') {
    if (
arg(0) == 'user' && is_numeric(arg(1)) && arg(2) == '') {
       
phptemplate_remove_tab('View', &$vars);
      }
  }
}
?>

This is just one of the menu related alterations we can do from the template.php file. But, it is one I put to good use regularly.

nice

nice. i'll add it as a comment to the end of my post.

http://nerdliness.com/article/2008/01/01/power-template-php

My account

Another short coming of Drupal 5 is the lack of ability to rename "My account". This is probably achievable through hook_menu_alter, but I was surprised to see it work with the Drupal 5 port of String Overrides....

You can move it, painfully

You can move it. But, it's kind of a hack. Make a menu item for the path /user and call it anything. My account will be automatically repositioned under this new menu item. Then move that and My account moves with it.

Thank you- this technique is

Thank you- this technique is just what I needed.

Nice trick. Thanks.

Nice trick. Thanks.

Default to "Edit" Tab

I modified the template.php code to the above suggestions and it worked great. I get rid of the "View" tab, but the Username and History information are still there. I would like for the user to default to the "Edit" tab so they can change their settings and not having to go to the View page, even though the tab is removed. Is it possible?

Also when I click on the "Edit" tab, the View tab reappears. How can I remove it?

Thanks!