In other words, what is the Drupal 8 equivalent of hook_menu_alter()?
Drupal 8 still uses hook_menu()
, but for what I can see, the information returned by the hook is different from what the hook returned in Drupal 7. For example, the definition given in user_menu()
for user is the following.
$items['user'] = array(
'title' => 'User account',
'title callback' => 'user_menu_title',
'weight' => -10,
'route_name' => 'user_page',
'menu_name' => 'account',
);
The route_name property links to an entry in the user.routing.yml file.
user_page:
pattern: '/user'
defaults:
_content: '\Drupal\user\Controller\UserController::userPage'
requirements:
_access: 'TRUE'
This is different from what done with Symphony, and it confuses me about how a module can alter the route defined from another user.
The only function that is still invoking hook_menu_alter()
is menu_router_build()
, but that function still contains code that needs to be updated, since it is still using the now deprecated drupal_alter()
.
// Alter the menu as defined in modules, keys are like user/%user.
drupal_alter('menu', $callbacks);
foreach ($callbacks as $path => $router_item) {
// If the menu item is a default local task and incorrectly references a
// route, remove it.
// @todo This may be removed later depending on the outcome of
// http://drupal.org/node/1889790
if (isset($router_item['type']) && $router_item['type'] == MENU_DEFAULT_LOCAL_TASK) {
unset($callbacks[$path]['route_name']);
}
// If the menu item references a route, normalize the route information
// into the old structure. Note that routes are keyed by name, not path,
// so the path of the route takes precedence.
if (isset($router_item['route_name'])) {
$router_item['page callback'] = 'USES_ROUTE';
$router_item['access callback'] = TRUE;
$new_path = _menu_router_translate_route($router_item['route_name']);
unset($callbacks[$path]);
$callbacks[$new_path] = $router_item;
}
}