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

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

To respond on your own website, enter the URL of your response which should contain a link to this post's permalink URL. Your response will then appear (possibly after moderation) on this page. Want to update or remove your response? Update or delete your post and re-enter your post's URL again. (Learn More)