[Drupal] How to remove undesired local task tabs from pages programmatically

| | 1 min read

In Drupal, some modules add tabs to pages and these tabs(Primary tabs) are shown in the top of each page for authenticated users. Some of these tabs are not needed for some roles. There are cases where we want to hide such tabs like, Bookmarks, File browser, Orders etc., from primary tabs for roles other than administrator. We can remove these undesired tabs from primary tabs programmatically.

Steps to remove undesired local task tabs from pages programmatically:

  1. First, we need to locate our theme's template.php file.
  2. Then we can use custom function to remove undesired tabs. The custom function name should start with your theme name for example, yourthemename_removetab(). The custom function can be as shown below:
    function yourthemename_removetab($label, &$vars) {
      // Remove from primary tabs
      if (is_array($vars['tabs']['#primary'])) {
        foreach ($vars['tabs']['#primary'] as $key => $primary_tab) {
          if ($primary_tab['#link']['title'] == $label) {
            unset($vars['tabs']['#primary'][$key]);
          }
        }
      }
    }
  3. Then, create a preprocess function - yourthemename_preprocess_page(). In which we can add conditions and call the above function to remove tabs as required. For example:
    function yourthemename_preprocess_page(&$vars) {
      if ($user->uid != 1 || !user_access('administer users')) {
        yourthemename_removetab('View', $vars);
        yourthemename_removetab('Bookmarks', $vars);
        yourthemename_removetab('File browser', $vars);
        yourthemename_removetab('My Channel', $vars);
        yourthemename_removetab('Orders', $vars);
        .....................
      }
    }

So, now we know how to remove undesired local task tabs from pages programmatically. Feel free to try this out and ask your questions as comments here.