19

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?

23

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',
       ),

    );
}
  • 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. – Mehrdad201 Jul 16 '18 at 18:29
  • I have added comments about hooks used in this code – werqious Jul 19 at 10:56
10

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.

  • thanks but installing additional modules is not appreciated thanks for this modules hacking module codes may give something helpful – werqious Jul 19 '12 at 11:34
  • @werqious updated answer – Andy Jul 19 '12 at 11:47
1

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.

1

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.