Add an extra view mode to an entity
The extra view mode is added to the entity by using hook_entity_info_alter. In this example the node entity is expanded so a hook_preprocess_hook theme function is used to add all available theme modes as template suggestions to the node. Templates like node--article--teaser.tpl.php and node--article--teaser-overview.tpl.php are now available.
<?php
/**
* @file:
* Drupal hooks and functions.
*/
define('VIEW_MODES_TEASER_OVERVIEW', 'teaser_overview');
/**
* Implements hook_entity_info_alter().
*/
function view_modes_entity_info_alter(&$entity_info) {
$entity_info['node']['view modes'][VIEW_MODES_TEASER_OVERVIEW] = array(
'label' => t('Teaser used on overview pages'),
'custom settings' => TRUE,
);
}
/**
* Implements hook_preprocess_HOOK().
*/
function view_modes_preprocess_node(&$variables) {
$variables['theme_hook_suggestions'][] = 'node__' . $variables['type'] . '__' . $variables['view_mode'];
}