Module Development

Define a Field Formatter Plugin for Drupal 8, 9, 10, and 11

Field formatters are responsible for taking the data stored by a field and transforming it into what a visitor sees. We can define new field formatters to output data for new field types or to provide alternative formatting options for existing field types. Creating a field formatter plugin is a common task for Drupal developers.

In this tutorial we'll:

  • Define a new background color field formatter that uses the string stored by our field_example_rgb field type as the background color of the output.
  • Make it possible for site builders to toggle on or off a feature that automatically adjusts the foreground text color of the output.

By the end of this tutorial you should be able to define a new custom field formatter plugin with settings that a site administrator can configure.