Community Documentation

Viewing entities

Last updated January 29, 2013. Created by fago on January 10, 2011.
Edited by eojthebrave. Log in to edit this page.

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 simliar to the 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.

Page status

No known problems

Log in to edit this page

About this page

Drupal version
Drupal 7.x
Audience
Programmers
Drupal’s online documentation is © 2000-2013 by the individual contributors and can be used in accordance with the Creative Commons License, Attribution-ShareAlike 2.0. PHP code is distributed under the GNU General Public License. Comments on documentation pages are used to improve content and then deleted.