New and noteworthy in Drupal 7 - View Modes

25 Oct

New and noteworthy in Drupal 7 - View Modes

in Custom View Mode, Drupal, Drupal, drupal 7, Drupal Planet, Drupal Planet

 

Drupal 6 offered two ways to display a node - teaser and full mode. However, There might be custom display types required - e.g. RSS display, search result or even home page display. Drupal 7 offers Custom View Modes to achieve custom rendering of the nodes.

You can create new view modes very easily using code or a module like Entity view modes

I will provide an overview of the code required to create a new view mode and defining a new node.tpl to display the node in the newly defined mode

 

To create a new custom view mode, implement the hook hook_entity_info_alter()

/**
* Implements hook_entity_info_alter().
*/
function my_modele_entity_info_alter(&$entity_info) {
$entity_info['node']['view modes']['rss'] = array(
'label' => t('RSS View'),
'custom settings' => TRUE,
);
}

We can define a node.tpl.php to leverage the above defined view mode

/**
* Implements hook_preprocess_node().
*/
function my_modele_preprocess_node(&$vars) {
if($vars['view_mode'] == 'rss') {
$vars['theme_hook_suggestions'][] = 'node__' . $vars['type'] . '__rss';
}
}

Name of the file will be node-article-rss.tpl.php

If you do not want to write code, you can also use the Display Suite module to achieve something similar. BTW, view modes is not the same as views module.

copyright 2012 10jumps Llc.

copyright 2012 10jumps LLC.