Skip to main content

Save time with View Modes

Front-end Development
Drupal

View modes are a great tool when theming a site, allowing you to display content in different ways across your site without having to create a custom template for each configuration and allowing you to configure your view displays easily. Drupal comes with two that you're probably used to seeing "Default" and "Teaser". But what if you wanted to create your own? You could download display suite which is a huge set of modules that does just that, or you could create your own custom module to create these new view modes in a few simple steps.

We're dealing with view modes so we're going to be using hook_entity_info_alter to programatically add the view modes to the node entity.

/*
* Implements hook_entity_info_alter().
*/

function modulename_entity_info_alter(&$entity_info) {
  $entity_info['node']['view modes']['grid'] = array(
    'label' => t('Grid'),
    'custom settings' => TRUE,
  );
   $entity_info['node']['view modes']['featured'] = array(
    'label' => t('Featured'),
    'custom settings' => TRUE,
  );
}

Here we're creating a "grid" view mode and a "featured" view mode that we'll call in our custom views display. That's all you'll need in your module file for this simple implementation. 

In your content configuration, make the changes you wish to display by clicking on our new view modes.

Here in our view we can simply call in the content display and choose our view mode, here we've chosen our new grid view mode. 

If you have any changes you need to make later on, you don't have to deal with adding and subtracting fields from the views interface, instead just modify your view mode in your content configuration with the changes you need to make and you're all set.

Need a fresh perspective on a tough project?

Let’s talk about how RDG can help.

Contact Us