logo

Palette - Make it Colorful🎨

Palette - Visual Page Builder, no design degree required.

Live Demo Download Palette

Scroll
04/10/2025, by Ivan

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

They are automatically discovered if placed in the expected directory structure and use the correct annotation.

The directory structure for a module that implements all three looks like this:

foo_bar_fields

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

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

The module will be named random and will follow this structure:

random

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