Skip to content

Instantly share code, notes, and snippets.

@sadanandkenganal
Last active August 29, 2015 14:02
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sadanandkenganal/873c9b00590d642265bd to your computer and use it in GitHub Desktop.
Save sadanandkenganal/873c9b00590d642265bd to your computer and use it in GitHub Desktop.
How to hide menu tabs, menu links and assign access permissions in Drupal 7

Hide menu tabs, menu links and assign access permissions in Drupal 7

hide "create new account" tab in "/user" page. To hide menu tabs we have to use 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.

###For hiding menu tabs

Eg: Hide 'create new account' tab  and 'request password' tab.

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

In your theme. File template.php
/**
 * Implements hook_menu_alter().
 *
 */
function themename_menu_alter(&$items) {
  $items['user/password']['type'] = MENU_CALLBACK;
}

###Access Permission

`The following implements hook_menu_alter to remove access to the "user/register" url for the anonymous user:`

/**
* Implements hook_menu_alter.
* Check access for forum menu item.
*/
function MYMODULE_menu_alter(&$items) {
  $items['forum']['access callback'] = '_accesscheck';
}

/**
* Callback to disallow access for the anonymous user.
*/
function _accesscheck(){
  global $user;
  // here you can check any user. Based on that,  return the value 0(False) or 1(True).
  return $user->uid;  /* Uid of anonymous is 0 */
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment