Viewing entities

Last updated on
14 October 2016

The entity API assists in writing the code for viewing an entity by providing entity_build_content() and entity_view() functions which take care of attaching any fields and works similar to the node_view() function.

The best way to provide your own custom content is to override the buildContent() method of your entity's controller class. Then, as long as you use the functions above to display your entities the changes will be picked up automatically.

For theming the entity API uses the basic entity.tpl.php template by default and adds some useful template suggestions similar to what core does for nodes.

{ENTITY TYPE}.tpl.php
{ENTITY TYPE}__{BUNDLE}.tpl.php
{ENTITY TYPE}__{BUNDLE}__{VIEW MODE}.tpl.php

Since the first suggestion is the name of the entity type you can simply define your custom template in your module using hook_theme() and it will be used automatically when displaying your entities.

An example from Profile2's hook_theme implementation:

..
    'profile' => array(
      'render element' => 'elements',
      'template' => 'profile',
    ),
..

The module can then provide a profile.tpl.php and it will be used whenever a profile entity is viewed.