in Web & Tech

Drupal Custom View Modes & Templates per View Mode

Just a quick reminder how to programmatically create your own Drupal 7 View Modes.
Replace superdrop_helpers with the name of your own module and listing with the desired name of your Drupal View Mode.

<?php 
/**
* Implements hook_entity_info_alter().
* Creates a new view mode called <listing>
*/
function superdrop_helpers_entity_info_alter(&$entity_info) {
  $entity_info['node']['view modes']['listing'] = array(
    'label' => t('Auflistung'),
    'custom settings' => TRUE,
  );
}

/**
* Implements hook_preprocess_node().
* Template file suggestion for the previously generated view mode
* e.g.: node--article--listing.tpl.php
*/
function superdrop_helpers_preprocess_node(&$vars) {
  if($vars['view_mode'] == 'listing') {
    $vars['theme_hook_suggestions'][] = 'node__' . $vars['type'] . '__listing';
  }
} 

Thanks to wunderkraut for their article on this topic, check it out if you need more explanation than just the snippet:
http://www.wunderkraut.com/blog/drupal-7-custom-node-view-modes/2010-12-20

Write a Comment

Comment