Skip to main content
Menu

Drupal 7 custom node view modes

Juha Niemi 2010-12-20

One of the cool new things in Drupal 7 is the ability to easily add new “View modes” for nodes. View modes are available for other entities like comments also, but I’m gonna only cover the nodes for this time. 

Before, Drupal had only two “hard-coded” node view modes, the full node view and the teaser view. These two provided us just enough flexibility for most simple content listings, but quite often we needed to turn to Views in Fields mode to get more flexibility. By flexibility in this case I mean displaying the same content in different ways in different contexts.

In Drupal 7 we can create additional view modes by simply implementing hook_entity_info_alter() in our custom module.

 



/**
* Implements hook_entity_info_alter().
*/
function MYMODULE_entity_info_alter(&$entity_info) {
$entity_info['node']['view modes']['another_teaser'] = array(
'label' => t('Another teaser'),
'custom settings' => TRUE,
);
}

 

After defining the new view mode, we can go to content type’s “Manage Display” page and select which fields to display plus some additional options for fields, like which image style to use for image fields, for example.






display_fields.jpeg




Next we need to get this view mode used somewhere. We’re gonna create a View for that. Set the View to Style = Unformatted, Row style = Node and select the “Another teaser” view mode from the row style options. Optionally you can create a page display with a path for the View for easier testing.






views-select-view-mode.jpeg




Up to this point nothing is that different than creating a view in “fields” mode, and select the desired fields one by one in the view itself. The differences are mostly how we can apply some theming and custom layout for the content.

To get the most out of this we’ll propably want to add a custom node.tpl.php template for this view mode. Custom node template enables easy and flexible theming and we can use standard hook_preprocess_node() function for example to control the variables we have available in the template. To make Drupal use a different .tpl.php file for view mode we need to add a new template suggestion in hook_preprocess_node().

/**
* Implements hook_preprocess_node().
*/
function MYMODULE_preprocess_node(&$vars) {
if($vars['view_mode'] == 'another_teaser') {
$vars['theme_hook_suggestions'][] = 'node__' . $vars['type'] . '__another_teaser';
}
}

Duplicate standard node.tpl.php for the content type you need, like node--article--another-teaser.tpl.php. (NOTE: use underscores in template suggestions, dashes in the filename).

There are many other ways to get the exactly same outcome, like the Display Suite module, but seems to be an overkill for a simple need what we just covered. While Views in fields mode is still most often the way to go, this opens a completely fresh option if you need to reuse the templates in multiple views, or you need to display every content type in a different way in the same listing without doing gigantic template files for views.