Sunteți pe pagina 1din 2

Drupal 7 custom node view modes | Wunderkraut

1 of 2

Drupal 7 custom node view modes


Monday , December 20, 2010 - 16:06
PLANET DRUPAL

DRUPAL 7

VIEWS

THEMING

TECHNOLOGY

One of the cool new things in Drupal 7 is the ability to easily add new View modes f or nodes. View modes are av ailable f or other entities
like comments also, but Im gonna only cov er the nodes f or this time.
Bef ore, Drupal had only two hard-coded node v iew modes, the f ull node v iew and the teaser v iew. These two prov ided us just enough
f lexibility f or most simple content listings, but quite of ten we needed to turn to Views in Fields mode to get more f lexibility . By f lexibility in
this case I mean display ing the same content in dif f erent way s in dif f erent contexts.
In Drupal 7 we can create additional v iew modes by simply implementing hook_entity _inf o_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,
);
}

Af ter def ining the new v iew mode, we can go to content ty pes Manage Display page and select which f ields to display plus some
additional options f or f ields, like which image sty le to use f or image f ields, f or example.

Next we need to get this v iew mode used somewhere. Were gonna create a View f or that. Set the View to Sty le = Unf ormatted, Row sty le
= Node and select the Another teaser v iew mode f rom the row sty le options. Optionally y ou can create a page display with a path f or the
View f or easier testing.

http://www.wunderkraut.com/blog/drupal-7-custom-node-view-modes...

7/25/2014 8:15 AM

Drupal 7 custom node view modes | Wunderkraut

2 of 2

Up to this point nothing is that dif f erent than creating a v iew in f ields mode, and select the desired f ields one by one in the v iew itself .
The dif f erences are mostly how we can apply some theming and custom lay out f or the content.
To get the most out of this well propably want to add a custom node.tpl.php template f or this v iew mode. Custom node template enables
easy and f lexible theming and we can use standard hook_preprocess_node() f unction f or example to control the v ariables we hav e
av ailable in the template. To make Drupal use a dif f erent .tpl.php f ile f or v iew 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 f or the content ty pe y ou need, like node--article--another-teaser.tpl.php. (NOTE: use underscores in
template suggestions, dashes in the f ilename).
There are many other way s to get the exactly same outcome, like the Display Suite module, but seems to be an ov erkill f or a simple need
what we just cov ered. While Views in f ields mode is still most of ten the way to go, this opens a completely f resh option if y ou need to
reuse the templates in multiple v iews, or y ou need to display ev ery content ty pe in a dif f erent way in the same listing without doing
gigantic template f iles f or v iews.

http://www.wunderkraut.com/blog/drupal-7-custom-node-view-modes...

7/25/2014 8:15 AM

S-ar putea să vă placă și