Skip to main content

Working with Drupal field formatters

Field formatters are how we control how the contents of a particular field gets displayed to the user, they are exposed under the display settings for your entity and can be as simple as configuring a date format right the way through to more complex, bespoke display requirements such as displaying images in a gallery format. Drupal ships with a number of field formatters out of the box and there's a wide range available in the form of contributed modules and of course it is possible to create your own! Find out more about field formatters by reading some of our blog posts below...

Read some of our articles about Drupal field formatters

Rendering fields in Drupal 9 (the right way)

Part of the series
Upgrading to Drupal 9
28th Jul 2020

Many of us at ComputerMinds have always taken pride on doing Drupally things the right way whenever possible, and then helping the community do so too. One of these things is displaying values from fields on content entities. We wrote before about how to do this in Drupal 7 and Drupal 8. It's now the turn of Drupal 9! Thankfully, this updated version is basically the same as the last one, as D9 is very similar to D8 on the surface, but with old cruft ripped out to allow it to continue improving. So the short answer to "How...

Read more

Rendering Drupal fields (the right way)

10th Apr 2018

Once upon a time, we wrote an article about how to render fields on their own in Drupal 7, which was really handy because Drupal 7 wasn't always intuitive. It's common to want to display a field outside of the context of its main entity page, like showing author information in a sidebar block or in a panel, but you had to just know which functions to use. Drupal 8 came along since then using 'grown up' things like objects and methods, which actually makes the job a little easier. So now for Drupal 9, 10 and beyond we have...

Read more

Drupal 8: Creating a custom field - Part 3: Field formatter

Jo Fitzgerald
11th Feb 2014

This is part 3 in my series of articles about creating a custom field. I recommend reading Part 1: Field type and Part 2: Field widget first, if you have not done so already.

After creating the field type and field widget it is now time to complete the set by creating the field formatter.

##a) Create the file
The field type must be located as follows:

<module_name>/lib/Drupal/<module_name>/Plugin/field/formatter/<field_formatter_name>.php
N.B. The field formatter name should be in CamelCase.

##b) Add Contains, namespace and use
In the newly created field type file add a brief comment to explain...</field_formatter_name></module_name></module_name>

Read more

Creating new field formatters in Drupal 7

3rd Feb 2011

Creating extra field formatters in Drupal 7 is fairly simple, but it can take some poking around in the new Fields code. So here is some sample code to make it easier for you! You can add formats to your own fields -- or existing fields that are defined by other Drupal modules. This is the case for the following example - a formatter for a link field to display the URL as an absolute URL.

First, implement hook_field_formatter_info() to declare your field formatter to the fields API, and implement hook_field_formatter_view() which tells Drupal what to do when viewing the...

Read more