function hook_menu_alter

7 system.api.php hook_menu_alter(&$items)
6 core.php hook_menu_alter(&$items)
8 system.api.php hook_menu_alter(&$items)

Alter the data being saved to the {menu_router} table after hook_menu is invoked.

This hook is invoked by menu_router_build(). The menu definitions are passed in by reference. Each element of the $items array is one item returned by a module from hook_menu. Additional items may be added, or existing items altered.

Parameters

$items: Associative array of menu router definitions returned from hook_menu().

Related topics

1 function implements hook_menu_alter()

Note: this list is generated by pattern matching, so it may include some functions that are not actually implementations of this hook.

comment_menu_alter in modules/comment/comment.module
Implements hook_menu_alter().
1 invocation of hook_menu_alter()
menu_router_build in includes/menu.inc
Collects and alters the menu definitions.

File

modules/system/system.api.php, line 1282
Hooks provided by Drupal core and the System module.

Code

function hook_menu_alter(&$items) {
  // Example - disable the page at node/add
  $items['node/add']['access callback'] = FALSE;
}

Comments

disable "Request new password" tab within the /user (login) page for one particular user.

function MODULE_menu_alter(&$items) {
  //condition to check the user
  global $user;
  if($user->id == '<USER_ID_OF_THE_USER>') { //can use $user->name as well
    $items['user/password']['access callback'] = FALSE;
  }
}

I am always appreciative of suggestions of how to use code snippets of hook_ functions.

However, I think this menu item only shows for anonymous users, users for whom user->id == 0 I believe that the 'user login' and the 'request new password' pages ( and menu items) are hidden by default for authenticated users, so this may not be a very good example.

The example above (posted by monymirza) is misleading and should be deleted.

  • it doesn't make sense, as this menu item only shows for the anonymous user (as already pointed out by jimurl.
  • hook_menu_alter doesn't run every time the menu loaded. It only runs when the cache is rebuilt and a few other times. I.e. yiu can't have logic in it, obly a constant value or a callback.
  • The $user->id is a typo (should be $user->uid).

The following implements hook_menu_alter to remove access to the "forum" tab 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;
  return $user->uid;
}

If you do not grant the anomymous user access to forum content, the anomymous user still has access to the forum landing page. This is rather pointless, so the example above is useful if you want to avoid this.

A lot of projects we do require hiding the standard "/node" page (you can change the front page, but /node stays active)

/**
* Restrict access to /node
*/
function MODULE_menu_alter(&$items) {
    $items['node']['access callback'] = FALSE;
}