如何从Drupal 8中的模块更改节点模板?
问题描述:
我想我的自定义Drupal 8模块为与某个URL匹配的页面强制使用不同的模板。这里是我的模块结构:如何从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();
}
当我参观页面的URL包含/builder/
,代码将运行并将字符串builder_template
添加到$suggestions
阵列。但是,当页面呈现时,新模板将被忽略。我曾尝试冲洗缓存,但没有结果。
我不确定如何继续。我发现的所有文档都是从定制主题添加主题建议,而不是自定义模块。
任何想法我做错了什么?非常感谢你的帮助!
答
也许您在粘贴的代码中存在拼写错误,但您的模板覆盖文件夹应为templates
而不是您编写的template
。不要忘记在更改目录名称后刷新缓存;)
您是否启用了Twig Debug?尝试$ suggestions [] = $ hook。 '__'。 'builder_template'; 枝条引擎有时会出现挂钩问题。 –