Field Formatters for Drupal 8, Drupal 9

Last updated November 18, 2020
Module Development8.9.x/9.0.x

Of the three main components of the field system -- types, widgets and formatters -- only one has an impact on the actual display of content for end users: field formatters. Field formatters are responsible for taking the data stored by our field and transforming it into the markup that is sent to the browser when an end user views our site.

In this tutorial we'll take a look at how field formatters are implemented. We'll look at the plugin type required to provide a field formatter, and see how one field type can support multiple formatters. We'll examine a detailed view of one of the formatters provided by Drupal core, and how to go about implementing a field formatter of our own in a custom module.

By the end of this tutorial, you should be able to:

  • Understand the role field formatters play in the Field API
  • Identify the main components that make up a field formatter
  • Be able to create a custom field formatter that includes configurable settings