换句话说,hook_menu_alter()的Drupl 8等效项是什么?
Drupal 8仍然使用hook_menu(),但是对于我所看到的,该钩子返回的信息与Drupal 7中返回的钩子不同。例如,user_menu()中为user 提供的定义如下。
$items['user'] = array(
'title' => 'User account',
'title callback' => 'user_menu_title',
'weight' => -10,
'route_name' => 'user_page',
'menu_name' => 'account',
);
route_name属性链接到user.routing.yml文件中的条目。
user_page:
pattern: '/user'
defaults:
_content: '\Drupal\user\Controller\UserController::userPage'
requirements:
_access: 'TRUE'
这与使用Symphony进行的操作不同,它使我困惑于模块如何更改另一个用户定义的路由。
仍在调用的唯一函数hook_menu_alter()
是menu_router_build(),但该函数仍包含需要更新的代码,因为它仍在使用现在不推荐使用的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;
}
}