37

node--[type|nodeid].tpl.php targets to node's default view mode. However I want to override the template for teaser view mode.

What is the template suggestion (.tpl.php file) for 'teaser' view mode?

57

I don't think there is one by default but you can easily add one in your template.php file:

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';
  }
}

That will let you use a template file like: node--[type|nodeid]--teaser.tpl.php

  • 3
    You could also just pull the variables straight from the array rather than referencing the node object too... – shaneonabike Dec 6 '14 at 1:23
1

There is a easier way to to this, through the Entity view modes module.

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

The template suggestion for a "teaser" view mode is:

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

By default the "teaser" view mode uses the regular node.tpl.php template, so you can copy that file to get started.

You can see all template suggestions by turning on theme_debug mode, https://www.drupal.org/node/223440#theme-debug

When you view-source: on the page you should see HTML comments that shows the entire list of template suggestions Drupal considered.

0

Clive's solution is correct. But if you want the new suggestions are evalueated after default suggestions, you must add them in the last positions of the array:

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');
  }
}

In this way you avoid that your teaser node matches (and uses, if it exists) node--[type].tpl.php before node--[type]--teaser.tpl.php

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.