hook_menu()
and hook_menu_alter()
are not always invoked; they are invoked only when Drupal rebuilds the menus, such as when a module is enabled, disabled, installed, or uninstalled, or when the Drupal cache is cleared.
Anatomy of hook_menu reports the following sentence:
hook_menu()
is called rarely, such as when modules are enabled. If you edit a module’s hook_menu()
, you must visit admin/build/modules for the changes to take effect.
The function that invokes the implementations of hook_menu()
and hook_menu_alter()
is menu_router_build()
, which contains the following code (in Drupal 7).
$callbacks = array();
foreach (module_implements('menu') as $module) {
$router_items = call_user_func($module . '_menu');
if (isset($router_items) && is_array($router_items)) {
foreach (array_keys($router_items) as $path) {
$router_items[$path]['module'] = $module;
}
$callbacks = array_merge($callbacks, $router_items);
}
}
drupal_alter('menu', $callbacks);
list($menu, $masks) = _menu_router_build($callbacks);
_menu_router_cache($menu);
return array($menu, $masks);
The function is called by menu_rebuild()
, which is then called by:
drupal_flush_all_caches()
is invoked by system_modules_submit()
, which is the submission handler for the module page where modules are enabled, and disabled; every time a module is enable, or disabled, the menu is rebuilt, and both the menu hooks are invoked.