By werqious


2012-07-19 06:06:58 8 Comments

I am creating a custom content type. By default, nodes support only two view modes (full and teaser):

function mymodule_view($node, $view_mode)
{
    if ($view_mode == 'full') {
         $node->content['#theme']= 'my_full_node_view_theme';
    }

    if ($view_mode == 'teaser') {
          $node->content['#theme']= 'my_teaser_node_view_theme'; 
    }
    return $node;
}

I want to add some other view modes for this node type, like:

  • small_box
  • small_box_with_user_pic
  • big_box

and want to render the node with code like this:

$node = node_load($my_nid);
$output = drupal_render(node_view($node, 'big_box'));

Any suggestions?

4 comments

@werqious 2012-07-19 13:05:37

first we have to add additional view modes with hook_entity_info_alter

function customuserblog_entity_info_alter(&$entity_info) {
     $entity_info['node']['view modes']['blog_post_big'] = array(
        'label' => t('simple big  teaser'),
        'custom settings' => TRUE,
      );
    }

// we may attach additional theme functions or templates and add variables with hook_view

function customuserblog_view($node, $view_mode) {
  if ($view_mode == 'blog_post_big') {
   // add some additional variables for template
    $node->content['#theme'] = 'custom_blog_big_teaser_view';
  }
}

// in our hook theme

customuserblog_theme(){
    return array(
      'custom_blog_big_teaser_view'= array(
          'render element' => 'form',
          'template' => 'custom-blog-big-teaser-view',
       ),

    );
}

@Mehrdad201 2018-07-16 18:29:11

I used your solution. it is good but this hook customuserblog_view($node, $view_mode) didn't work. so I used hook_preprocess_node with function anonymous_profile_preprocess_node(&$vars) in order to make custom tpl for custom view mode.

@werqious 2019-07-19 10:56:51

I have added comments about hooks used in this code

@FranCarstens 2015-03-25 18:00:35

I know this is an older topic, but I have found the following method to work just fine for most use cases.

These simple steps will take you through creating your own module with the new view mode. It's fairly simple. I would love to provide attribution, but I can't remember where I found the basis for this. It does follow the same logic as werqious's answer though.

File 1 : my_module_view_modes.module

<?php
//Add more view modes for content type displays, in addition to default and teaser.
function almagest_view_modes_entity_info_alter(&$entity_info) {

//NB: media_ prefix required.
//You can repeat the following section for any view modes you'd like to create.

// First View Mode
// tag 1 references the entity type, ex. node or file
// tag 3 provides a machine name for your mode
  $entity_info['node']['view modes']['my_view_mode'] = array(
    'label' => t('My View Mode'), // This is what you'll see in your "Manage Display" tab.
    'custom settings' => TRUE,
  );

// Another View Mode    
  $entity_info['file']['view modes']['my_other_view_mode'] = array(
    'label' => t('Another View Mode'),
    'custom settings' => TRUE,
  );
}

File 2 : my_module_view_modes.info

name = My Module View Modes
description = Add additional "View Modes" for entities in this module. Helpful for additional displays in views or node rendering.
package = My Modules
version = 7.x - 0.1
core = 7.x

Save these two files in your my_module_view_mode folder in your modules folder and enable. Clear your cache and now you'll see the new view modes in their respective entities.

@Andre Baumeier 2015-03-30 13:12:36

how is this different than drupal.stackexchange.com/a/37488/13366?

@Alex Skrypnyk 2013-05-28 04:58:49

If you are using Display Suite, make sure that ds_ui module is enabled and go to admin/structure/ds/view_modes to get a list of existing and create new view modes.

@Andy 2012-07-19 08:37:09

If all you want is custom view modes, then Entity View Mode can help. Display Suite also makes it easy to create custom view modes, to create new pseudo-fields, and have a nice drag & drop interface for laying out different elements in the various view modes.

If you want to do this all in code, then entity_example from the Examples module has a view mode IIRC. Drupal Commerce also has a number of custom entities with custom view modes.

@werqious 2012-07-19 11:34:18

thanks but installing additional modules is not appreciated thanks for this modules hacking module codes may give something helpful

@Andy 2012-07-19 11:47:40

@werqious updated answer

Related Questions

Sponsored Content

1 Answered Questions

View modes are stuck on default view

2 Answered Questions

[SOLVED] Add a custom attribute to the main div of a node only on teaser view mode

  • 2017-10-10 12:36:47
  • Shakil Ahmad
  • 57 View
  • 0 Score
  • 2 Answer
  • Tags:   7 theming

1 Answered Questions

[SOLVED] Managing different view modes for a single content type

  • 2013-11-20 22:41:06
  • alouette
  • 1092 View
  • 0 Score
  • 1 Answer
  • Tags:   7 entities

4 Answered Questions

[SOLVED] Render fields in node template with different view mode

  • 2013-05-28 10:51:43
  • Santosh
  • 5617 View
  • 3 Score
  • 4 Answer
  • Tags:   7 entities nodes

3 Answered Questions

[SOLVED] How to provide template for particular view mode?

  • 2012-03-30 16:56:07
  • artfulrobot
  • 28620 View
  • 47 Score
  • 3 Answer
  • Tags:   7 theming entities

1 Answered Questions

How to assigned specific "comment" view mode for specific "node" view mode?

1 Answered Questions

[SOLVED] Images not shown in View Mode Teaser

  • 2014-07-29 07:26:36
  • moertle
  • 590 View
  • 0 Score
  • 1 Answer
  • Tags:   7 theming nodes

1 Answered Questions

1 Answered Questions

[SOLVED] How to define another teaser for the second theme?

Sponsored Content