节点“预告”视图模式的模板建议是什么?


37

node-[type | nodeid] .tpl.php指向节点的默认查看模式。但是我想覆盖预告片查看模式的模板。

“预告片”查看模式的模板建议(.tpl.php文件)是什么?

Answers:


57

我认为默认情况下没有一个,但是您可以轻松地在template.php文件中添加一个:

function MYTHEME_preprocess_node(&$vars) {
  if($vars['view_mode'] == 'teaser') {
    $vars['theme_hook_suggestions'][] = 'node__' . $vars['node']->type . '__teaser';   
    $vars['theme_hook_suggestions'][] = 'node__' . $vars['node']->nid . '__teaser';
  }
}

这样您就可以使用如下模板文件: node--[type|nodeid]--teaser.tpl.php


3
您也可以直接从数组中拉出变量,而不是直接引用节点对象...
shaneonabike 2014年

1

通过“实体”视图模式模块,有一种更简单的方法。

https://www.drupal.org/project/entity_view_mode

The Drupal 7 successor to Build modes which will allow administrators to 
define custom view modes for entities. Custom entities are added to the 
entity registry via hook_entity_info_alter() so they are available to any code
that uses entity_get_info() to provide a list of view modes for an entity. 
This includes node and user reference fields, Views, etc.

It also ensures consistency for template suggestions for all entity types, 
so that you can use any of the template patterns, in order of most specific 
to least specific:

entity-type__id__view-mode
entity-type__id
entity-type__bundle__view-mode
entity-type__bundle
entity-type

1

“预告片”查看模式的模板建议是:

node--[type]--teaser.tpl.php

默认情况下,“前导”视图模式使用常规node.tpl.php模板,因此您可以复制该文件以开始使用。

您可以通过打开theme_debug模式https://www.drupal.org/node/223440#theme-debug查看所有模板建议

当您在页面上查看源代码时:您应该看到HTML注释,该注释显示了Drupal考虑的模板建议的整个列表。


0

克莱夫的解决方案是正确的。但是,如果您希望新建议在默认建议之后进行评估,则必须将它们添加到数组的最后位置:

function MYTHEME_preprocess_node(&$vars) {
  if($vars['view_mode'] == 'teaser') {
    array_unshift($vars['theme_hook_suggestions'], 'node__' . $vars['node']->type . '__teaser');
    array_unshift($vars['theme_hook_suggestions'], 'node__' . $vars['node']->nid . '__teaser');
  }
}

这样,您可以避免预告节点匹配(并使用,如果存在)节点-[type] .tpl.php优先于节点-[type]-teaser.tpl.php

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.