【发布时间】: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