Custom templates for particular node view modes

Previously I've discussed how you can render different teaser and full node code via one template file. This doesn't work though if you want seperate template files, or want to create a template for a custom view mode that you've created.

Just in case you are wondering what a view mode is, there are two you should already be familiar with, the full node & teaser view. Using the Display Suite module you can create additional view modes. These view modes can then used in a variety of ways - for example in Views or when printing a node with node_load().

To create a content template for a particular view mode you will need to utilise the hook_preprocess_HOOK function via your theme's template.php file.

In this example I've got a profile content type, and have created a custom view mode with Display Suite called Author bio (/admin/structure/ds/view_modes). Below are the machine names:

  • node type = profile
  • view mode = author_bio

The template file for this can be created from a clone of the default node.tpl.php file, and will use this naming convention ->   node--[nodeType]-[viewMode].tpl.php

  • node--profile-author_bio.tpl.php  

(Note how the the dashes and underscores are used, and how they correspond with what is in the template.php code below!)

​Add this to the theme's template.php file.

// Implementation of hook_preprocess_HOOK().
function custom_preprocess_node(&$vars) {
    if ($vars['node']->type == 'profile' && $vars['view_mode'] == 'author_bio') {
        $vars['theme_hook_suggestions'][] = 'node__profile_author_bio';
Drupal version: 
Justin Chevallier

Justin Chevallier

Avid Drupal site builder & user for +10 years.

Add new comment

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.