Breaking

Followers

Friday, 16 June 2017

Create custom node view modes


In Drupal 7 we can create additional view modes by simply implementing hook_entity_info_alter() in our custom module.

/**
* Implements hook_entity_info_alter().
*/
function MYMODULE_entity_info_alter(&$entity_info) {
  $entity_info['node']['view modes']['another_teaser'] = array(
    'label' => t('Another teaser'),
    'custom settings' => TRUE,
  );
}


To get the most out of this we’ll propably want to add a custom node.tpl.php template for this view mode. Custom node template enables easy and flexible theming and we can use standard hook_preprocess_node() function for example to control the variables we have available in the template. To make Drupal use a different .tpl.php file for view mode we need to add a new template suggestion in hook_preprocess_node().

/**
* Implements hook_preprocess_node().
*/
function MYMODULE_preprocess_node(&$vars) {
  if($vars['view_mode'] == 'another_teaser') {
    $vars['theme_hook_suggestions'][] = 'node__' . $vars['type'] . '__another_teaser';
  }
} 

No comments:

Post a Comment