Field types, widgets, and formatters are plugins

Last updated on
8 May 2021

In Drupal 8, field types, widgets, and formatters are all plugins.

They are automatically discovered if present in the expected directory and using the proper annotation.

The directory structure of a module that implements all three looks as follows:

foo_bar_fields

  • foo_bar_fields.info.yml
  • src/
    • Plugin/
      • Field/
        • FieldType/
          • FooItem.php
        • FieldFormatter/
          • FooBarFormatter.php
        • FieldWidget/
          • FooBarWidget.php

In this guide, we will create a custom field type, field formatter and field widget that generates a random string and displays it.

The name of the module will be random and it will contain the following structure:

random

  • random.info.yml
  • src/
    • Plugin/
      • Field/
        • FieldType/
          • RandomItem.php
        • FieldFormatter/
          • RandomDefaultFormatter.php
        • FieldWidget/
          • RandomDefaultWidget.php

Help improve this page

Page status: No known problems

You can: