Creating a custom Field

Last updated on
21 September 2016

In this guide we will go through the following topics:

  • Creating a custom field type
  • Creating a custom field formatter
  • Creating a custom field widget

All three tasks require implementing a plugin that belongs to the Field module.

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