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:
- Link
- Date
- Entity Reference
- Field Collection fields (treated like Entity Reference fields)
- Paragraphs fields (treated like Entity Reference fields)
- Addressfield
- Telephone
- Viewfield (Patch needed: href="https://www.drupal.org/node/2451779">https://www.drupal.org/node/2451779)
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
- Official Documentation
- Video / Blog Post explaining the principles of Entity Formatter
Project information
- Minimally maintained
Maintainers monitor issues, but fast responses are not guaranteed. - 22 sites report using this module
- Created by valderama on , updated
- Stable releases for this project are covered by the security advisory policy.
Look for the shield icon below.
Releases
Development version: 7.x-1.x-dev updated 8 Dec 2017 at 11:13 UTC