drupal - 如何将操纵器添加到现有菜单

标签 drupal menu drupal-8

我什至不知道如何问这个问题。在下面的代码(我从示例中找到)中,除了默认操纵器之外,我还想添加另一个操纵器以删除同级外部的所有链接。

$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/

相关文章:

德鲁巴 8 : How to stop drupal stocking sessions?

php - 德鲁巴 8 : How do i fix the irritating error message "Unable to install advertisement, already exist in active configuration"?

mysql - 将数据库从一台主机移动到另一台主机时出现 phpMyAdmin 错误

jquery - 每行具有不同背景图像的水平菜单

c++ - 从另一个窗口显示系统菜单

macos - 从OSX上Electron的Finder中删除菜单栏

drupal - 自定义 block 在 Drupal 8 中不起作用

php - Drupal 7 中用户的图片存储在哪里?

Drupal7 : content type with many fields

php - 通过表单将文件上传到 Drupal 7 后,如何获取文件的路径?