[Solved][Drupal] How to hide menu tabs and menu links in Drupal 7

| | 2 min read

Drupal module uses a lot of functionality. Once we enable a module we add many functionalities to Drupal. Sometimes we do not want to display the functionality provided by module but we do want the functionality to work in the background. For example creating a new account is functionality that can be hidden from users. This can be done by hiding some tabs or links in Drupal. Read on to know how to hide existing menu tabs in Drupal.

Why we need to hide menu tabs

You know the Drupal user module comes with a lot of functionality, one is "create new account". This is a cool stuff but here we are creating an account without admin's involvement. But if we want to make a user account only after a requesting mail from user or after paying some amount to admin?. Then we have to disable the "create new account" functionality provided by "user" module.

one.png

Hiding menu tabs

Here we are going to hide "create new account" tab in "/user" page. To hide menu tabs we have to hook_menu_alter hook. This hook changes the menu items. So here we are going to alter menu tabs that come with user module. Using this hook we can also do some other changes like title, permissions etc.

By default MENU_NORMAL_ITEM will be a menu type. But in /users page menu displays as tabs. Here it uses MENU_LOCAL_TASK and MENU_DEFAULT_LOCAL_TASK.

So to take the link out of the menu we are going to use MENU_CALLBACK. It simply register a path so that the correct function is fired when the URL is accessed. They do not appear in menus or breadcrumbs. And this is what we are looking for.

To hide the menu tabs we can write the following code.


/**
 * Implements hook_menu_alter().
 *
 */
function MYMODULE_menu_alter(&$items) {
  $items['user/password']['type'] = MENU_CALLBACK;
}

two.png

This code will not display a "Create new account" menu tab in the page "/user". We can use this code in anywhere in our custom module. Also we can hide "Request new password" as well as other existing menu tabs in the same way.

To remove "Request new password" menu tab we can use

$items['user/register']['type'] = MENU_CALLBACK;

I hope this information helps you. Thank you