If the standard node view modes are insufficient, you can use existing modules like Display Suite and Entity view mode. However, these modules can be large and complex, and it’s not always practical to use them. To add a custom view mode, Drupal provides the hook hook_entity_info_alter().
Let’s add a new display type called gallery_teaser
:
/**
* Implements hook_entity_info_alter().
*/
function MODULENAME_entity_info_alter(&$entity_info) {
$entity_info['node']['view modes']['gallery_teaser'] = array(
'label' => t('Gallery teaser'),
'custom settings' => TRUE,
);
}
After activating this module, “Gallery teaser” will appear in the display settings.
You can also use this new view mode in Views.
Next, we need to support standard template names like node--[type]--[viewmode].tpl.php
:
/**
* Implements hook_preprocess_node().
*/
function MODULENAME_preprocess_node(&$vars) {
/**
* Example template name - node--article--gallery-teaser
* Use underscores in template suggestions, dashes in the filename.
*/
if ($vars['view_mode'] == 'gallery_teaser') {
$vars['theme_hook_suggestions'][] = 'node__' . $vars['type'] . '__gallery_teaser';
}
}
Now, you can customize the appearance of the node in the node--article--gallery-teaser.tpl.php
template.
Important: underscores (_
) in view mode and content type names must be replaced with dashes (-
) in template filenames.