Drupal 7: how to hide unwanted tabs

DrupalAssuming you want to hide some tabs from a specific page, content type, user role, specific URLs or other discriminating criteria, you need to work on the $variables variable and unset the unneeded tabs.

The following snippet will remove “subscriptions” and “favorites” tabs to all the non “editor” users:

if($variables['user']->uid > 1 && !in_array('editor', $variables['user']->roles) && !in_array('administrator', $variables['user']->roles)) {
 $to_be_removed = array('user/%/subscriptions', 'user/%/favorites');
 foreach ($variables['tabs'] as $group_key => $tab_group) {
  if (is_array($tab_group)) {
   foreach ($tab_group as $key => $tab) {
    if (isset($tab['#link']['path']) && in_array($tab['#link']['path'], $to_be_removed)){
     unset($variables['tabs'][$group_key][$key]);
    }
   }
  }
 }
}

Enjoy!

Advertisements

9 thoughts on “Drupal 7: how to hide unwanted tabs

  1. If you look at the comment here (https://www.drupal.org/node/1869774#comment-9843749) you would replace [YOUR-MODULE-NAME] with the name of your custom module, and the name of your custom module is completely arbitrary. You could call it ‘fein’, for example (but it’s better to use a name that describes what it does, or use something general like thiswebsite_helper). Check the link I supplied for how to create a custom module, including what has to go into the .info file, and what has to go in the .module file. Your structure would look something like this:

    in /sites/all/modules/custom, you could create a fein directory with two main files in it like so:
    /sites/all/modules/custom/fein
    /sites/all/modules/custom/fein/fein.module
    /sites/all/modules/custom/fein/fein.info

    Inside the .module file you could paste in the two functions I mentioned, so it would look something like:

    <?php
    /**
    * Implements hook_permission().
    *
    * Create custom permission to allow visibility for the subscription tab
    */
    function fein_permission() {
    return array(
    'can view subscription tab' => array(
    'title' => t('Can view subscription tab'),
    'description' => t('Allows access to the subscription tab on the user account page'),
    ),
    );
    }

    /**
    * Implements hook_menu_alter().
    *
    * Show or hide the Subscriptions tab on the user profile page
    * based on a custom permission
    */
    function fein_menu_alter(&$items) {
    $items['user/%user/subscriptions']['access callback'] = 'user_access';
    $items['user/%user/subscriptions']['access arguments'] = array('can view subscription tab');
    }

    You’d then activate this module on your website and now you can use the functionality! If you’re anxious about it, I would just give it a try and see what happens.

  2. If you put it into template.php then only your front-end theme will pick up this change. If you are accessing this tab on any admin pages, it’s best to put it into a custom module. That way, it will be processed whether you’re viewing the tab in the admin theme or the public-facing theme. Creating a custom module is super easy, not daunting at all. For more info on the comment I listed, see https://www.drupal.org/node/1074360 for a tutorial on creating a custom module.

    • Thank you for your response. I prefer this method too but I’m not sure what function to use the snippet with.

Your opinion is important!

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s