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?

5 comments

@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?

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

first we have to add additional view modes

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

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.

@Alex.Designworks 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

@aroo 2012-07-19 07:37:43

Here is the presentation from Tim Cosgrove about view modes: http://timcosgrove.net/drupalcon-viewmodes/#1

You will use hook_entity_info_alter to add a new view modes (example).

@niksmac 2012-11-26 07:40:29

That presentation was awesome.

@Kari Kääriäinen 2016-01-27 22:26:10

The page is gone. Slides are here though: slideshare.net/Phase2Technology/…

Related Questions

Sponsored Content

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
  • 54 View
  • 0 Score
  • 2 Answer
  • Tags:   7 theming

4 Answered Questions

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

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

2 Answered Questions

Hide default value by view mode

1 Answered Questions

[SOLVED] Custom node template with related node teasers

  • 2013-01-23 00:03:25
  • summer
  • 71 View
  • 1 Score
  • 1 Answer
  • Tags:   theming

3 Answered Questions

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

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

1 Answered Questions

[SOLVED] Create node template only for default view-mode

  • 2015-08-26 14:14:18
  • sam
  • 315 View
  • 0 Score
  • 1 Answer
  • Tags:   7 theming

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
  • 547 View
  • 0 Score
  • 1 Answer
  • Tags:   7 theming nodes

1 Answered Questions

[SOLVED] How to get available view modes per content type?

  • 2014-02-25 12:44:04
  • wucherpfennig
  • 775 View
  • 0 Score
  • 1 Answer
  • Tags:   7 entities

0 Answered Questions

node_view() doesn't populate the "content" variable when using a custom view mode

  • 2012-01-14 01:19:41
  • BC01
  • 300 View
  • 0 Score
  • 0 Answer
  • Tags:   entities

Sponsored Content