Drupal 7 custom node view modes

Public

"One of the cool new things in Drupal 7 is the ability to easily add new “View modes” for nodes. View modes are available for other entities like comments also, but I’m gonna only cover the nodes for this time. "

See there for more details : http://www.wunderkraut.com/blog/drupal-7-custom-node-view-modes/2010-12-20

Add this code to enable another_display in node display settings, in views and in node.tpl.php :

</> CopyGet raw version
php
  1. /**
  2. * Implements hook_entity_info_alter().
  3. */
  4. function MYMODULE_entity_info_alter(&$entity_info) {
  5. $entity_info['node']['view modes']['another_teaser'] = array(
  6. 'label' => t('Another teaser'),
  7. 'custom settings' => TRUE,
  8. );
  9. }

Use the code bellow allow themers to create node--another-teaser.tpl.php file

</> CopyGet raw version
php
  1. /**
  2. * Implements hook_preprocess_node().
  3. */
  4. function MYMODULE_preprocess_node(&$vars) {
  5. if($vars['view_mode'] == 'another_teaser') {
  6. $vars['theme_hook_suggestions'][] = 'node__' . $vars['type'] . '__another_teaser';
  7. }
  8. }

Comments

Anonymous's picture

Add this code... to where?

Rhetorical: Since when is changing code considered easy?