Field Types

Plugins can provide custom field types by creating a class that implements craft\base\FieldInterface (opens new window) and craft\base\FieldTrait (opens new window). The class will serve both as a way to communicate various things about your field type (with static methods), and as a model that fields of its type will be instantiated with.

As a convenience, you can extend craft\base\Field (opens new window), which provides a base field type implementation.

You can refer to Craft’s own field classes for examples. They are located in vendor/craftcms/cms/src/fields/.

# Registering Custom Field Types

Once you have created your field class, you will need to register it with the Fields service, so Craft will know about it when populating the list of available field types:

namespace mynamespace;

use craft\events\RegisterComponentTypesEvent;
use craft\services\Fields;
use yii\base\Event;

class Plugin extends \craft\base\Plugin
    public function init()
            function(RegisterComponentTypesEvent $event) {
                $event->types[] = MyField::class;

        // ...

    // ...

# Supporting Delta Saves

If your field type does any processing from afterElementSave() (opens new window) or afterElementPropagate() (opens new window), you can improve performance by skipping processing when the field’s value is unchanged.

You can tell whether field content has changed by calling isFieldDirty() (opens new window) on the element.

public function afterElementSave(ElementInterface $element, bool $isNew)
    if ($element->isFieldDirty()) {
        // logic for handling saved element

    parent::afterElementSave($element, $isNew);

# Storing Content

By default, a field type’s hasContentColumn() (opens new window) method returns true, meaning Craft will create one or more fields for it in the database’s content table.

The field type’s getContentColumnType() (opens new window) method can return either a single column type, or a key-value array of multiple handles and types (opens new window).

public function getContentColumnType()
    return \yii\db\Schema::TYPE_STRING;

See Craft’s Date (opens new window) field for an example that uses two columns when it’s configured to store a timezone.

Any column Craft creates in the content table will automatically get a random 9-character suffix like _ycpsotpa. This ensures column names are unique even in the rare case that identical handles are used. You can get this suffix from any field instance from its columnSuffix property.

This automatic suffix behavior was introduced in Craft 3.7 and only impacts existing fields when they’re saved—not via migration when Craft is updated.

If you’d rather have your custom field type manage its own content in the database, you can return false for hasContentColumn()—just know that you’ll have to manually account for several things you otherwise get for free when Craft manages its content fields in the database:

  • Automatic creation, duplication, retrieval, and deletion of field content in the database.
  • Any core migrations or improvements that impact the content table.
  • Automatic support for sites and localization.
  • Automatic inclusion of content fields in element queries and GraphQL.