Template driven formatting

Entity Formatter can generate custom PHP classes for your entities, which make theming of the entities and its fields easier.

Access your field values in an easy way and format and print them in your template. Regain full control about your field markup and CSS classes!

For example you could use this code in your node--page.tpl.php file:

<?php
  // Create the entity formatter object.
  $page = new PageNodeFormatter($node);
?>
<article class="page">

  <section class="page__summary">
    <?php
      // Access each field via a get method,
      // and use convenient formatting methods depending on the field type.
      print $page->getBody()->summary(250);
    ?>
  </section>
  <section class="page__images">
    <?php
      // You can easily iterate over multi value fields.
      foreach ($page->getImages() as $image) {
        print $image->setImageStyle('thumbnail')->img();
        
        // Want more control? You can just get the src path with any image style!
        print '<img src="' . $image->src() . '" class="my-image-class">';
      }
    ?>
  </section>
</article>

Why should I use this module?

The main benefits are:

  • Place your formatting logic in your entity template.
  • Fully control your markup and CSS classes in your entity template.
  • Easy access of formatted and semi-formatted field values.
  • Make use of code completion tools in your IDE for printing fields.

Supported fields

Currently all core fields are supported plus some special fields:

Drush

The module provides a single drush command "entity-formatter-make" or short "efm".

drush efm node --bundles=page,article --module=entity_formatter_custom

This example generates formatter classes for page and article node types and place them in the entity_formatter_custom module (generating module if not exists).

Language

By default the current content language is used for printing the fields.

If you use Entity Translation you need to patch Entity API to make multilingual fields to work with Entity Formatter (Patch here: https://www.drupal.org/node/2335357).

Related

The code to generate the classes is taken from Wrappers Delight module, which was also a great source of inspiration for this module.

PHP Version

PHP 5.3 or higher required.

Resources

Supporting organizations: 
sponsors development.

Project Information

Downloads