Template Suggestions for the Entity Module

Many Drupal 7 modules are built on top of the invaluable Entity Module which makes much easier to implement custom entity types in Drupal.  Truly great for module developers!  However, like many Drupal modules, the documentation of the module is sparse to say the least.  This is especially true when it comes to theming.  Thankfully, the entity modules also makes theming a breeze.

 

When creating a new template for an entity built using the entity module we can use the following template suggestions:

 

  1. [entity type].tpl.php
  2. [entity-type]--[entity bundle].tpl.php
  3. [entity type]--[entity bundle]--[view mode].tpl.php
  4. [entity type]--[entity id].tpl.php

 

As an example, say we're using the Profile2 Module to have user editable profiles.  If we have a profile type called "Member" (with a machine name of member) it would allow us to use the following template suggestions:

 

  1. profile2.tpl.php
  2. profile2--member.tpl.php
  3. profile2--member--default.tpl.php (or any other view mode which can be added by other modules)

 

This gives us a great degree of flexibility for theming entities and can make your development life a lot easier.

 

A Quick Note Regarding Preprocessing

The entity module also exposes a nice preprocessing hook for us, hook_entity_preprocess(&$variables).  Inside of the variables array you'll find the same properties that can help you using the following keys:

 

  • entity_type - The type of the entity (e.g. profile2)
  • view_mode - The view mode of the entity (e.g full, teaser, etc.)

 

Along with many more properties that you can use the theme your content.  Happy hacking!