Remove a Drupal tab

Posted Feb 22, 2008 // 21 comments
Frank:

Have you ever been looking at a page on a Drupal site (like the login page) and wanted to remove a pesky tab (like the “Request a new password” tab), but could not figure out how to do it?

Me neither, until recently. I went spelunking through the Drupal and theming core and uncovered some gems. All the items that are to be tabs are specified as MENU_LOCAL_TASK in the specific hook_menu implementation. And there is a global way to intercept all theming for those types of items. You can them look them up, check their path(or any other piece of data) and decide whether or not to render it. See below where I wanted to get rid of a tab with the URL user/password


function phptemplate_menu_local_task($mid, $active, $primary) {
     //Check each tab being rendered for our victim    
     $item = menu_get_item($mid);
     if ($item['path'] == 'user/password') {
         return '';
     }
     //The rest is copied from theme_menu_local_task()
     if ($active) {
         return '<li class="active">'. menu_item_link($mid) ."</li>";
     }
     else {
         return '<li>'. menu_item_link($mid) ."</li>";
     }
}

Voila!

About Frank

Frank Febbraro is the CTO at Phase2. He is primarily interested in software, technology and integrating new techniques and practices with proven methods and approaches. A combination of inherent understanding and real world experience enables ...

more >

Read Frank's Blog

Comments

by Anonymous (not verified) on Fri, 03/28/2008 - 16:24

Funny how I can get LDAP

Funny how I can get LDAP authentication to work, but I still can’t get rid of that darn “Request new password” tab on the user login page…

Where do you put this fuction, and where is it called from?

TIA

by febbraro on Wed, 04/02/2008 - 21:23

You can put it in 2 places

You can either put the function in your theme, in the template.php file, or in any old module that you write. You can write a custom module just for this if you need to.

by Anonymous (not verified) on Mon, 12/22/2008 - 02:47

So, did it work for you in

So, did it work for you in the end? I tried to follow these steps, but the tabs are still there. Have this issue in a few places, like the search page, where node and user modules insert unwanted tabs. The user pages have tabs that are hard to remove too.

by febbraro on Mon, 12/22/2008 - 10:49

Still works.

This has worked for me on a number of occasions. You can also get fancy and use hook_preprocess_page to modify the $tabs variable as a last resort.

To get rid of the Search Node and User tabs you can use the Core Search module. http://drupal.org/project/coresearches

Good luck,
Frank

by Anonymous (not verified) on Sun, 05/04/2008 - 05:05

Can't Make it work! :(

I’ve found this same ‘fix’ on this page: http://drupal.org/node/68792 but I can’t seem to make it work! I am needing to remove the ‘search/node’ but it does nothing when I add it to my template.php file…

by Phil (not verified) on Sun, 05/04/2008 - 05:17

Figured out the problem...

K, you have to replace ‘phptemplate_’ with whatever your theme name is…

That removed the tab, but only hid it. This still doesn’t solve my problem though : (

Back to the research grindstone!

by febbraro on Mon, 05/05/2008 - 11:46

What is the problem?

What exactly is the problem you are trying to solve? Are you trying to not only hide the tab but remove that URL entirely from the system?

by Anonymous (not verified) on Fri, 08/01/2008 - 14:54

How did you remove the tab?

I followed all the steps but my “Request new Password” tab is still there.

by Anonymous (not verified) on Sun, 06/01/2008 - 20:06

Do you know how to make

Do you know how to make this work in Drupal 6? The code above doesn’t seem to work.

by febbraro on Mon, 06/09/2008 - 16:46

Roughly the same should work

Roughly the same approach should work

http://api.drupal.org/api/function/theme_menu_local_task/6

But consider overriding it with [themename]_menu_local_task instead of phptemplate_menu_local_task. The theming system may have changed to not allow a global override like that.

by nonsie (not verified) on Thu, 09/04/2008 - 20:57

In D6 you will need to do

In D6 you will need to do smth like this in your template.php:

function mytheme_menu_item_link($link) {
  if($link['href'] == 'user/password') {
    return '';
  }
  else {
    if (empty($link['localized_options'])) {
      $link['localized_options'] = array();
    }
    return l($link['title'], $link['href'], $link['localized_options']);
  }
}

by shubha (not verified) on Tue, 04/14/2009 - 03:15

did'nt work for D6

Even this does not work in drupal 6!!

by Tirdad on Tue, 06/02/2009 - 11:32

$link

That won’t work because $link contains HTML and is not an array. Here is how I got it to work on Drupal 6:

<?php
function yourtheme_menu_local_task($link, $active = false) {
  if (
strpos($link, 'The name of the tab I want to remove')) {
    return
'';
  } else {
    return
'<li '. ($active ? 'class="active" ' : '') .'>'. $link ."</li>\n";   
  } 
}
?>

by Anonymous (not verified) on Tue, 01/13/2009 - 04:22

Also, for both D5 & D6, make

Also, for both D5 & D6, make sure you clear your menu cache each time when testing.

by febbraro on Tue, 01/13/2009 - 08:58

Yes!

We can't forget the ritualistic clearing of the caches. Thanks. :)

by Anonymous (not verified) on Mon, 02/09/2009 - 12:04

I followed all the steps but

I followed all the steps but my “Request new Password” tab is still there.

by seddonym (not verified) on Wed, 08/19/2009 - 05:05

Got it working for D6

Here's how I got it working for removing the Revisions tab - a bit clunky but you just search the end of the string function YOURTHEME_menu_local_task($link, $active = FALSE) { if (substr($link, -13) == 'Revisions</a>') { return ''; } else { return '>li '. ($active ? 'class="active" ' : '') .'<'. $link ."</li>\n"; } }

by seddonym (not verified) on Wed, 08/19/2009 - 05:10

Oops, that should

Oops, that should read: function YOURTHEME_menu_local_task($link, $active = FALSE) { if (substr($link, -13) == 'Revisions</a>') { return ''; } else { return '<li '. ($active ? 'class="active" ' : '') .'>'. $link ."</li>\n"; } }

Sorry, can't for the life of me figure out how to put line breaks in this code...

by kratib@drupal.org (not verified) on Thu, 09/10/2009 - 18:58
by Anonymous (not verified) on Sun, 01/17/2010 - 20:09

Really? And how, pray tell, would you do that?

I think you should read the APIs before offering up a solution. This function does NOT provide what's needed to achieve the result described. It's for modifying menu data before it's saved into the database, not before it is rendered on the screen. Unless you want to completely remove the tabs from the entire system, this is not the way to do it.

by michael (not verified) on Wed, 06/16/2010 - 18:27

chill out anonymous 'Really? And how...'

Take it easy, bro :) That was actually the answer I was looking for.

Post new comment

The content of this field is kept private and will not be shown publicly.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
  • Allowed HTML tags: <a> <strong> <code> <p> <img> <ul> <ol> <li> <h2> <h3> <h4> <b> <u> <i>
  • You may insert videos with [video:URL]

More information about formatting options

CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.