Last updated June 7, 2014. Created on July 29, 2013.
Edited by cosmicdreams, intervalle, targoo. Log in to edit this page.

This page is outdated and needs rewriting. The hook_field_info didn't exist anymore in D8 and is replaced by the use of a field.field.[entity_type].[field_name].yml file in your custom module.

Let's walk through the process of creating a field type, a field widget, and a field formatter for Drupal 8.

Let's look at the directory structure of the custom module that will provide the new field:

dogefield <-- module name
src
Plugin
Field

  • Names and describes the field
  • Establishes a connection to the the field formatter and field widget we'll write later.
  • Create a custom field formatter

    Create a custom field widget

    Hooks required for a field formatter

    The following list of hooks are required to implement a custom field.

    Widget

    Formatter

    1)Use annotation to define your formatter.

    /**
    * Plugin implementation of the 'module_plain' formatter.
    *
    * @FieldFormatter(
    *   id = "module_plain",
    *   module = "module",
    *   label = @Translation("Plain"),
    *   field_types = {
    *     "module_field"
    *   }
    * )
    */

    2) Create your class which extends FormatterBase

    Examples

    • Email (core module)
    • Telephone (core module)
    • Color Field

    Looking for support? Visit the Drupal.org forums, or join #drupal-support in IRC.