Drupal 7 Entity View Modes Module

By shane
Fri, 2014-08-01 06:40
Daily Dose of Drupal Episode #161

Share with Others

The Drupal 7 Entity View Modes Module allows you to define custom view modes for your entities. A view mode allows you to configure which fields on your entity you want to display. For example, Drupal by default has a Teaser view mode that you can set up to display different fields from your Full Content view mode. This module will allow you to add your own view modes on top of the ones Drupal gives you by default.

In this lesson you will learn:

  • What entity view modes are and how they can be used
  • How to use the Entity View modes module to add a new view mode to a content type (node entity)

Hello everyone and welcome to another Daily Dose of Drupal. Today we’re on Episode Number 161. I am Shane Thomas, you can follow me on Twitter at smthomas3, also make sure to check out Code Karate and Code Karate recently released ebook.

Today we’re going to be going over the Entity View Mode Module, and this is really going to be a really simple and quick video today because there’s not a lot to it but it is very useful in certain situations. Essentially, it lets you allow additional View or Build Modes for your entities.

And so this could be a situation where you need to display specific entities, maybe it’s a specific types of content in multiple ways and we’ll go ahead and show you how that works and maybe give you a few ideas of how you can use it but there’s a lot of different ways which you may need this. So your typical use case, you probably don’t need it too often but there are certain situations where it’s handy to have a couple extra Build Modes for your content types or for different types of entities.

First step is to download the module of course and then make sure you have it on. As you can see I already have it enabled here. So you can click Configure from the modules page or you can go to Configuration System Entity View Modes to get to the Entity View Modes Configuration page. You’ll see here it has enlisted all of the different entities on your Drupal site.

So there’s Comment Fieldable Panel Pane, Mailchimp Sign Up Node and this is the most common one that you are all aware of, Taxonomy terms, User. Any type of entity that you have on your site, you can add different View Modes for. So we’ll go ahead and adding a new Node View Mode. Before we do, we go to our content type Manage Display page for the article content type and you can see there’s a default and teaser sub tabs here for different View Modes.

You can also turn other ones on such as a Search Index, Search Results, so there are other ones that come out of the box that you can use and turn on, but there may be times where you want to add new View Modes here. So you can go ahead and do that by going back to the Entity View Mode, clicking Add New View Mode, you can select, first give it a name. We’ll just go ahead and say we’ll call this “The Simple View Mode”. Maybe you won’t have a slim down. It’s not a teaser, maybe it has a little more information than a teaser but it isn’t quite the full content display.

This is more useful for complex content types or complex entities with multiple fields where you may want to have different ways to display the pieces of content or the entity where you may have a teaser, or you may have a full content, you may have something in between or a different type or slightly different layout or structure to the fields.

Here you can check if you want to use Customs Display Settings, we’ll leave that checked, you can specify which content types here. You want to have it enabled for so we’ll just use article for now. We will click save and now we’ll up on this list right here. So it’s simple, it says it’s in the database because we created it. It’s not stock that did not come with Drupal. Custom Settings, yes, you can add it, you can delete it.

So if we go to our content type, we go to basic page first just to show you that it’s not here by default. However you still can turn it on by checking this box and clicking Save. If we go to the Article Content Type, you’ll notice that the simple View Mode has been turned on. So you can see default, we have image, body tags, teaser, maybe we want just the image. And maybe simple is going to contain the image and the body. So now you have three different View Modes here for your Article Content Type.

Now you may not necessarily see how this can be useful but you can use View Modes or the Build Modes in a bunch of different ways. For instance in Views, so we’re going to go ahead and create a new View Quick of our articles, we’ll create one called Test Article List, we’ll show content of type article and we’ll just use this page that the defaults do.

So unformatted a list of Teasers, we don’t need links, no comments and we will click Continue and Edit. So let’s go ahead and click Save and take a look at the page of Teasers first. Here it just shows the image. So in this case the Teaser only shows the actual image field. If we edit the view we can of course change this to full content or our simple View Mode that we just created.

So let’s go ahead and use the simple View Mode and if we click Save, and now you see it shows the image and the body. If we went to the full content it would show the image, the body and then any links that were with that content. So as you can see, it’s really simple, it allows you to add additional flexibility to your various entities. Keep in mind it’s not just for content, you can use it for any entity in your system.

Perhaps you want different displays for your user accounts depending on how you want them to be laid out or shown on different areas of your site. Maybe you want blocks that show different users on your site in a different way. There’s a whole bunch of things you can do with it, it’s flexible, it integrates in with Views easily as you saw, you could of course have done the same thing just using Fields with Views but sometimes it’s nice to be able to pre-configure specific displays and use it in that way.

So go ahead and give the Entity View Modes a look if you need it and also it said it ensures consistency for template suggestions. So it’s easy to use different templates to change how those View Mode look. So go ahead and give it a try and let me know what you think. We’ll see you next time.