如何在Drupal 8中更改模块中的节点模板?

时间:2016-09-23 16:08:05

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

我希望我的自定义Drupal 8模块为与特定网址匹配的网页强制使用不同的模板。这是我的模块结构:

/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();
}

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

我不知道该怎么办。我发现的所有文档都是指从自定义主题添加主题建议,而不是自定义模块。

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

1 个答案:

答案 0 :(得分:1)

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

相关问题