1

我正在使用hook_theme_suggestions_HOOK_alter()根据内容类型为 page.html.twig 添加主题挂钩建议:

function mytheme_theme_suggestions_page_alter(array &$suggestions, array $variables) {
  $node = \Drupal::request()->attributes->get('node');
  $suggestions[] = 'page--node--' . $node->getType();
}

现在我的 twig 调试模式选择了这个:

    <!-- FILE NAME SUGGESTIONS:
   * page--node--case.html.twig (new suggestion based on content type 'case')
   * page--node--3.html.twig
   * page--node--%.html.twig
   * page--node.html.twig
   x page.html.twig
   -->

但是,当我创建一个名为 的文件时page--node--case.html.twig,它不会被渲染。相反,page.html.twig正在使用。

有谁知道发生了什么?

4

1 回答 1

5

我发现出了什么问题。

显然,在定义新建议时,Drupal 需要下划线而不是破折号。然后 Drupal 将这些下划线转换为破折号,这样实际的文件名仍然是 page--node--case.html.twig

所以: $suggestions [] = 'page--node--'.$node->gettype();

应该: $suggestions [] = 'page__node__'.$node->gettype();

文档: https ://www.drupal.org/node/2186401

于 2015-08-18T14:22:26.267 回答