Drupal 8 - template_preprocess_node 不工作

Drupal 8 - template_preprocess_node not working

我创建了一个带有视图模式 "Teaser Full Width" 的文章列表视图。该页面看起来不错,并且列出了文章。当我尝试使用 template_preprocess_node(&$variables) 编辑某些内容时,挂钩在 Teaser Full Width 中不起作用。但是,当我单击任何文章时,单个节点中的工作相同。

<?php
function templatename_preprocess_node(&$variables) {
   kint($variables);die('test');
}
?>

我已经在 Whosebug 中解决了不同的问题。

  1. 开发模块已启用。
  2. 已尝试 if ($variables['view_mode'] == 'teaser_full_width'){ ... }
  3. 尝试 print_r 而不是 kint
  4. 已尝试更改 max_allowed_packet
  5. 已验证 templatename 已启用并设置为默认值。
  6. 缓存已清除。

(文章列表页面) 模板文件:- node--article--teaser-full-width.html.twig

template_preprocess_html 在此页面上运行良好。

template_preprocess_node 此页面不工作。

(文章单页) 模板文件:- node--article--full.html.twig

两个挂钩都工作正常。

有人知道问题出在哪里吗?我的 drupal 版本是 8.6.10.

根据 drupal 的文档,template_preprocess_node() 是节点模板的挂钩。由于视图列表页面包含节点但不是节点页面,因此 template_preprocess_node() 在那里不起作用。但是,它仍然是一个 html 页面,因此 template_preprocess_html() 在那里工作。

简单来说,您的 "article listing view" 是一个页面而不是节点,因此 template_preprocess_node() 不起作用。