Site Building
Topic

Fields and the Field API for Drupal 7, 8, 9, and 10

Fields are the building blocks of Drupal's powerful content modeling system. The Field API allows for the development of custom field types, field widgets, and field formatters to suit almost any data display and collection needs. When combined with entities, which allows for the attachment of fields to entity bundles (commonly known as content types), Drupal becomes infinitely flexible without requiring writing any code.

Example tasks

  • Understand how the main components of Drupal's field system fit together
  • Learn how fields can be used to customize your site
  • Customize existing fields and create new additional functionality within your own modules

Confidence

At this point in the development cycle, the Field API is stable. Contributed modules that provide additional field types and add functionality to the field system are likely to emerge, but the main components of the core functionality are unlikely to change.

Drupalize.Me resources

Fields for site builders

More information

Overview of content entities and fields.

Drupal 7, 8, 9, 10, and 11
More information

Content modeling consists of identifying and constructing the different building blocks of data that compose the information on your site.

Field API for developers

Categories
Drupal 8, 9, 10, and 11
More information

If you've ever created or edited a piece of content on a Drupal site you have already interacted with the Field API. The Field module (along with its user interface counterpart) is responsible for providing the forms and data components used to build up the content model that make up a Drupal site. Understanding how Drupal fields work and how they're constructed via the Field API is an important part of understanding how Drupal works.

In this tutorial, we're going to look at the main components that make up the Field API at a high level. We'll see how the Field UI module exposes the field types included in core. We'll also look at the three main pieces that compose fields: types, widgets and formatters.

Categories
Drupal 8, 9, 10, and 11
More information

Drupal's field system provides us with a flexible system of adding different types of discrete data to content types. This enables us to create rich content models. The Field API allows us to define these distinct field types by creating a new plugin. These plugins specify a FieldType attribute. In this tutorial, we'll look at these attributes in detail. We'll look at the implementations of field types from Drupal core. Also, we'll see what a new custom field type would look like.

By the end of this tutorial, you should be able to:

  • Understand how field type definitions are created and exposed to Drupal
  • Identify the various field types provided by Drupal core
  • Understand the requirements for providing a specification for a new field type
Categories
Drupal 8, 9, 10, and 11
More information

Once we've explored the various field types provided by Drupal core, the next component of the Field API to explore is field widgets. Field widgets define how the data structure of the field is displayed on an edit form. When content editors interact with your field type they will be doing so via the field widget you provide. In this tutorial, we'll take a look at the field widgets provided by Drupal core, the plugin type required to define our own custom widget and how we can define multiple widget options for a single field type.

By the end of this tutorial, you should be able to:

  • Understand where field widgets fit into the overall Field API
  • Implement a custom field widget to support a particular field type
Categories
Drupal 8, 9, 10, and 11
More information

Of the 3 main components of the field system -- types, widgets and formatters -- only 1 has an impact on the actual display of content for end users: field formatters. Field formatters are responsible for taking the data stored by a field and transforming it into the markup that is sent to the browser when an end user views your site.

In this tutorial we'll:

  • Look at the role field formatters play in the Field API
  • Identify the main components that make up a field formatter

By the end of this tutorial you should be able to define the role of a field formatter plugin.

Categories
Drupal 8, 9, 10, and 11
More information

Drupal's Field API specifies the implementation details for field types, widgets and formatters. It also provides several hooks that allow custom code to alter these implementation details. In this tutorial we'll take a look at these Field API hooks and see how they can be used to change field types, widgets and formatters.

By the end of this lesson, you should be able to:

  • Identify existing Field API hooks for manipulating field behavior
  • Understand the proper method for changing the behavior of a field type, widget or formatter
  • Know where to find the documentation for these API functions, and how to find their implementations

Fields and Entities are closely related concepts. It is best to learn both.

Drupal 7, 8, 9, 10, and 11
More information

Entities are the fundamental building blocks that make up any Drupal site. Having a good understanding of the Entity system is an important part of ensuring the data model of your Drupal site is set up properly.

Drupal 7

Guides

Not sure where to start? Our guides provide useful learning tracks for all skill levels.

Navigate guides

External resources

  • FieldTypes, FieldWidgets, and FieldFormatters (Drupal.org)
    • Overview of field types, widgets, and formatters for developers. Includes a handy list of all field types in Drupal core
  • Field API Documentation (api.drupal.org)
  • Field Types API Documentation (api.drupal.org)
    • Together these two links constitute the main API documentation pages for the Field and Field Types APIs. The other pages linked to from here, including the widget and formatter APIs, cover the bulk of the functionality behind Drupal's field system.
  • Creating a custom Field (Drupal.org)
    • This tutorial, along with others in progress from drupal.org, walks through the process of creating a custom field.
  • Examples for Developers (Drupal.org)
    • The Examples project contains an example module (field_api) with sample code demonstrating adding a new field type, widget, and formatter.