Skip to main content

Custom custom Drupal entity view modes (without the bloat!)

Published on

When it comes to Drupal I love entities. Entity display modes provide simplicity in managing how your content is displayed. I used to be hardcore "Fields only" when it came to using Views and displaying content. Yeah, it gives you more control, but it is a pain in the ass when you have to replicated your field settings multiple times (if you have to.) Wouldn't it be super easy to just say "Hey Views, render this entity!" and BAM it is all there for you.

Problem is that out of the box your display view modes are limited - I mean who can get by on just Default and Teaser. There are modules that can provide you the UI to create a new custom view mode, but chances are there's a lot of extra weight in doing so. Is it worth that universal contrib module taking up your code base for one extra view mode? Probably not.

Luckily it is insanely easy to create your own view modes. All you need to do is just create a simple module that invokes hook_entity_info_alter() and add your view mode to the proper array - dead simple.

How simple? Review this GitHub Gist, which includes adding a theme_hook_suggestion for your display mode:

That's it. Turn it on and now you can add in your extra view modes and get your site buildin' on.

I'm available for one-on-one consulting calls – click here to book a meeting with me 🗓️