繁体   English   中英

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

[英]How do I alter a node template from a module in Drupal 8?

我希望我的自定义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数组中。 但是,呈现页面时,新模板将被忽略。 我尝试刷新缓存,但没有结果。

我不确定如何进行。 我发现的所有文档都涉及从自定义主题(而不是自定义模块)添加主题建议。

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM