我什至不知道如何问这个问题。在下面的代码(我从示例中找到)中,除了默认操纵器之外,我还想添加另一个操纵器以删除同级外部的所有链接。
$manipulators = array(
array('callable' => 'menu.default_tree_manipulators:checkAccess'),
array('callable' => 'menu.default_tree_manipulators:generateIndexAndSort'),
// This is what i want to do. Remove all links outside of siblings and active trail
array('callable' => 'mytheme.menu_transformers:removeInactiveTrail'),
);
将此“removeInactiveTrail”方法放置在哪个类中的什么位置?
最佳答案
如果您是 drupal 新手,总是很难提出正确的问题。但是,如果您已经走到这一步并找到了适合您任务的代码片段,那么最好对 core 和 contrib 模块进行研究,以了解其他人如何使用这些函数和方法。
如果您不确定实现情况,只需添加有关您想要实现的目标的更多详细信息。
以下是您可以在自定义模块中使用的示例:
function mymodule_render_menu($menu_name) {
$menu_tree = \Drupal::menuTree();
// Build the typical default set of menu tree parameters.
$parameters = $menu_tree->getCurrentRouteMenuTreeParameters($menu_name);
// Load the tree based on this set of parameters.
$tree = $menu_tree->load($menu_name, $parameters);
// Transform the tree using the manipulators you want.
$manipulators = [
// Add your manipulators here
];
$tree = $menu_tree->transform($tree, $manipulators);
// Finally, build a renderable array from the transformed tree.
$menu = $menu_tree->build($tree);
return array('#markup' => render($menu));
}
上面的函数返回可渲染数组。您可以从 hook_preprocess_HOOK 调用它,添加到变量数组中并在模板中输出。
同样,您的任务不明确,请将您的问题编辑得更具体。
关于drupal - 如何将操纵器添加到现有菜单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47878647/