Backend Development - Field Types

Fields are one of Drupal’s most important backend concepts. They define how data is stored, validated, and exposed across entities.

In this topic, we move beyond using existing fields and learn how to create custom field types. This is where backend Drupal starts to feel like a data modeling framework rather than just a CMS.

We continue the learning-by-building approach by extending the same weeklydrupal_demo module. The goal is to understand not just how to create a field type, but why Drupal structures fields the way it does.

By the end of this article, you will understand:

  • What a field type is
  • How field data is stored
  • The core classes involved in field types
  • How Drupal uses typed data and plugins
  • When custom field types are appropriate

1. What Is a Field Type

A field type defines:

  • The structure of the data
  • How the data is stored in the database
  • How the data is validated

Examples of field types you already know:

  • Text
  • Integer
  • Boolean
  • Entity reference

Each of these is implemented as a field type plugin.


2. When to Create a Custom Field Type

Create a custom field type when:

  • Data does not fit existing field types
  • Multiple values must be stored together
  • Validation rules are specific and reusable
  • The data should be queryable and indexed

Do not create a custom field type just to change display. That is handled by field formatters.


3. Field Type Architecture (High Level)

A field type is made up of:

  • A field type plugin
  • A data definition (typed data)
  • A database schema

Drupal uses this information to:

  • Create database tables
  • Validate input
  • Expose data to Views, REST, and APIs

4. Field Type Plugin Basics

Field types are plugins.

They live in:

src/Plugin/Field/FieldType

Plugin discovery is handled by annotations.


5. Creating a Custom Field Type

We will create a simple field type called rating.

File path

weeklydrupal_demo/src/Plugin/Field/FieldType/RatingItem.php

Code

<?php

namespace Drupal\weeklydrupal_demo\Plugin\Field\FieldType;

use Drupal\Core\Field\FieldItemBase;
use Drupal\Core\Field\FieldStorageDefinitionInterface;
use Drupal\Core\TypedData\DataDefinition;

/**
 * Plugin implementation of the 'rating' field type.
 *
 * @FieldType(
 *   id = "rating",
 *   label = @Translation("Rating"),
 *   description = @Translation("Stores a numeric rating."),
 *   default_widget = "rating_widget",
 *   default_formatter = "rating_formatter"
 * )
 */
class RatingItem extends FieldItemBase {

  public static function propertyDefinitions(FieldStorageDefinitionInterface $field_definition) {
    $properties['value'] = DataDefinition::create('integer')
      ->setLabel(t('Rating value'));

    return $properties;
  }

  public static function schema(FieldStorageDefinitionInterface $field_definition) {
    return [
      'columns' => [
        'value' => [
          'type' => 'int',
          'unsigned' => TRUE,
          'size' => 'tiny',
        ],
      ],
    ];
  }

  public function isEmpty() {
    $value = $this->get('value')->getValue();
    return $value === NULL;
  }

}

6. Understanding the Annotation

@FieldType(
  id = "rating",
  label = @Translation("Rating"),
  description = @Translation("Stores a numeric rating."),
  default_widget = "rating_widget",
  default_formatter = "rating_formatter"
)

Explanation:

  • id – Machine name of the field type
  • label – Display name in UI
  • description – Explanation for admins
  • default_widget – Input method
  • default_formatter – Display method

These connect the field type to widgets and formatters.


7. propertyDefinitions()

Defines the data structure of the field.

DataDefinition::create('integer')

This tells Drupal:

  • What type of data is stored
  • How it should be validated
  • How it integrates with Typed Data API

8. schema()

Defines how the data is stored in the database.

'columns' => [
  'value' => [
    'type' => 'int',
  ],
],

Drupal uses this to create field storage tables automatically.


9. isEmpty()

public function isEmpty() {
  $value = $this->get('value')->getValue();
  return $value === NULL;
}

Drupal calls this method to decide:

  • Whether the field has data
  • Whether validation should run
  • Whether empty values should be stored

10. How Field Types Fit Into Drupal

Field types:

  • Define storage
  • Power widgets and formatters
  • Integrate with Views
  • Support REST and APIs

They are foundational to Drupal’s data model.


11. Learning by Building: weeklydrupal_demo

Our demo module now includes:

  • A custom field type
  • Typed data definitions
  • Database schema integration

Next topics will build on this by adding:

  • Field widgets
  • Field formatters
  • Entity CRUD operations

12. Common Mistakes to Avoid

  • Creating field types for display-only needs
  • Hardcoding database queries
  • Skipping validation logic
  • Using generic data types when structure matters

Field types should model data accurately.


13. Why Field Types Matter

Understanding field types helps you:

  • Design better content models
  • Build reusable data structures
  • Integrate with APIs cleanly
  • Scale complex data requirements

This is a key backend skill for Drupal developers.