【问题标题】:How do I alter a node template from a module in Drupal 8?如何从 Drupal 8 中的模块更改节点模板?
【发布时间】:2016-09-23 16:08:05
【问题描述】:

我希望我的自定义 Drupal 8 模块为匹配某个 URL 的页面强制使用不同的模板。这是我的模块结构:

/sass_edit
    /css
    /js
    /template
        builder_template.html.twig // this is the template file
    sass_edit.module
    ...
    sass_edit.*.yml // various yml files

这些是builder_template.html.twig的内容:

<h1>test</h1>

以下是我的.module 文件中的相关代码行:

function sass_edit_theme_suggestions_alter(array &$suggestions, array $variables, $hook) {

  $current_path = \Drupal::service('path.current')->getPath();

  if(\Drupal::currentUser()->isAuthenticated() && strpos($current_path, "/builder/") >= 0) {
    $suggestions[] = 'builder_template';
  }

  // kint($suggestions);
  // die();
}

当我访问 URL 包含 /builder/ 的页面时,代码会运行并将字符串 builder_template 添加到 $suggestions 数组中。但是,在呈现页面时,新模板将被忽略。我试过刷新缓存,没有结果。

我不确定如何继续。我找到的所有文档都是指从自定义主题添加主题建议,而不是自定义模块。

知道我做错了什么吗?非常感谢您的帮助!

【问题讨论】:

  • 您是否启用了 Twig 调试?试试 $suggestions[] = $hook 。 '__' 。 'builder_template'; Twig Engine 有时会出现挂钩建议的问题。

标签: drupal twig drupal-modules drupal-theming drupal-8


【解决方案1】:

也许您在粘贴的代码中有拼写错误,但您覆盖模板的文件夹应该是templates,而不是您所写的template。更改目录名称后不要忘记刷新缓存;)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多