当同一节点应用于多个菜单项时, inActiveTrail 为 对于第一个菜单而不是当前菜单,请单击它。
出于测试目的,在drupal 8站点中创建多级菜单并应用以下代码,它将输出当前的活动菜单数组。只需省略$currentlinkId
和$parameters->setRoot
即可看到数组中的inActiveTrail
。
$menu_tree = \Drupal::menuTree();
$menu_name = 'main';
$parameters = $menu_tree->getCurrentRouteMenuTreeParameters($menu_name);
$currentLinkId = reset($parameters->activeTrail);
$parameters->setRoot($currentLinkId);
$tree = $menu_tree->load($menu_name, $parameters);
// Transform the tree using the manipulators you want.
$manipulators = array(
// Only show links that are accessible for the current user.
array('callable' => 'menu.default_tree_manipulators:checkAccess'),
// Use the default sorting of menu links.
array('callable' => 'menu.default_tree_manipulators:generateIndexAndSort'),
);
$tree = $menu_tree->transform($tree, $manipulators);
kint($tree);