Field formatter
A field formatter is the Drupal plugin that turns a field's stored values into page output, such as plain text or the label of a referenced item.
A field formatter is the part of a field that controls how its values are displayed when the entity is shown. You pick it per field and per view mode on the Manage display page. A long text field can be shown trimmed or in full, and a term reference as plain text or as a link, depending on the formatter.
How field formatters work in Drupal 11
A field has a type, and that type can name a default formatter. The Field UI lets a site builder choose another formatter that supports the type. For each view mode the page offers a Label setting (whether and how to show the label), a formatter drop-down, a gear icon for the formatter's settings, and a drag handle for the order of the fields. Choosing Hidden in the formatter drop-down, or dragging the field into the Disabled section, removes the field from that view mode.
In code, a formatter is a plugin:
- It carries the
FieldFormatterattribute with anid, alabeland afield_typeslist of the field types it supports. - It implements
FormatterInterface, usually by extendingFormatterBase, and lives inDrupal\{module}\Plugin\Field\FieldFormatter. - Its
viewElements()method receives the field items and a language code and returns a render array with one child per value, keyed by consecutive numbers from 0. Theview()method builds the fully themed field, including the label. settingsForm()andsettingsSummary()provide the gear-icon form and the one-line summary.isApplicable()tells whether the formatter fits a given field definition.
Core examples: the string formatter (labeled Plain text) supports the string and uri field types and has a setting to link the output to the entity. The entity_reference_label formatter (labeled Label) displays the label of the referenced entities. Contributed modules can add more formatters. The Field API describes the plugin system as a whole.
Example
The Drupal User Guide improves the display of a Vendor content type. Go to Structure › Content types, open the menu beside Vendor and choose Manage display. In the Label column, select Hidden for Main image and Vendor URL. Click the gear icon of Vendor URL, clear the link text trimming and check Open link in new window. Click Update, reorder the fields, and click Save. The guide says to clear the cache if the changes do not appear.
Common mistakes
- Changing only the default display. A view mode that uses custom settings ignores the default. Check the Custom display settings section and switch to the view mode you are looking at, such as teaser.
- Mixing up the two Hidden options. The Label setting hides the label; the formatter drop-down hides the field.
- Looking for the setting on the edit form. The edit form belongs to the widget. A formatter works on the output of the page.
- Not seeing a change. Clear the cache before concluding that the setting failed, as the user guide advises.
For marketing and content teams
Formatters decide how the same content looks in different places: full in an article, shortened in a list. If a field shows too much, too little or the wrong label, the fix can be a formatter setting rather than a content edit. Ask your site builder to change it on Manage display. If no formatter produces the output you want, a developer can write one as a plugin.
With DXPR
DXPR Builder is a field formatter. On Manage display you change the Format of a formatted text field, such as Body, from Default to DXPR Builder. After that, every item of the content type opens the visual builder for editors who have permission. The setup guide lists the steps. See DXPR Builder.
Sources
- Drupal User Guide: Concept: View Modes and Formatters
- Drupal User Guide: Changing Content Display
- Drupal core 11.4.8: Field UI help text (FieldUiHooks.php)
- Drupal core 11.4.8: Field Formatter API topic (field.api.php)
- Drupal core 11.4.8: FormatterInterface
- Drupal core 11.4.8: FieldFormatter attribute
- Drupal core 11.4.8: StringFormatter
- Drupal core 11.4.8: EntityReferenceLabelFormatter
- DXPR Builder 3.0.0-beta23: DxprBuilderFormatter
- DXPR Builder: Use DXPR Builder on your content types