Last updated July 25, 2016. Created on July 29, 2013.
Edited by mian3010, thursday_bw, Eyal Shalev, romainj. Log in to edit this page.

Field types define the properties and behaviours of fields

To create a field type in Drupal 8 you need a class with the FieldType annotation.

The location of the field type class should be placed inside MODULE_NAME/src/Plugin/Field/FieldType
/modules/foo/src/Plugin/Field/FieldType/BazItem.php

The namespace of that class should be \Drupal\MODULE_NAME\Plugin\Field\FieldType
\Drupal\foo\Plugin\Field\FieldType\BazItem

The annotation above the class, within the doc comment, should include a unique id, and a label.

/**
 * Provides a field type of baz
 * 
 * @FieldType(
 *   id = "baz",
 *   label = @Translation("Baz field"),
 * )
 */

The class needs to implement the FieldItemInterface interface. And can extend the FieldItemBase class for common implementation of the interface.

class BazItem extends FieldItemBase implements FieldItemInterface {

  /**
   * {@inheritdoc}
   */
  public static function schema(FieldStorageDefinitionInterface $field_definition) {
    return array(
      'columns' => array(
        'value' => array(
          'type' => 'text',
          'size' => 'tiny',
          'not null' => FALSE,
        ),
      ),
    );
  }

  /**
   * {@inheritdoc}
   */
  public static function propertyDefinitions(FieldStorageDefinitionInterface $field_definition) {
    $properties['value'] = DataDefinition::create('string');

    return $properties;
  }

}

The TextWidget from the field_example module under the examples project:


/**
 * @file
 * Contains Drupal\field_example\Plugin\Field\FieldType\RgbItem.
 */

namespace Drupal\field_example\Plugin\Field\FieldType;

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

/**
 * Plugin implementation of the 'field_example_rgb' field type.
 *
 * @FieldType(
 *   id = "field_example_rgb",
 *   label = @Translation("Example Color RGB"),
 *   module = "field_example",
 *   description = @Translation("Demonstrates a field composed of an RGB color."),
 *   default_widget = "field_example_text",
 *   default_formatter = "field_example_simple_text"
 * )
 */
class RgbItem extends FieldItemBase {
  /**
   * {@inheritdoc}
   */
  public static function schema(FieldStorageDefinitionInterface $field_definition) {
    return array(
      'columns' => array(
        'value' => array(
          'type' => 'text',
          'size' => 'tiny',
          'not null' => FALSE,
        ),
      ),
    );
  }

  /**
   * {@inheritdoc}
   */
  public function isEmpty() {
    $value = $this->get('value')->getValue();
    return $value === NULL || $value === '';
  }

  /**
   * {@inheritdoc}
   */
  public static function propertyDefinitions(FieldStorageDefinitionInterface $field_definition) {
    $properties['value'] = DataDefinition::create('string')
      ->setLabel(t('Hex value'));

    return $properties;
  }
}

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

Comments

estoyausente’s picture

How can I save automatically the creaded date?

I mean, I have this schema definition:

  /**
   * {@inheritdoc}
   */
  public static function schema(FieldStorageDefinitionInterface $field_definition) {
    $columns = [
      'columns' => [
        'value' => [
          'description' => 'The ID of the referenced proveedor.',
          'type' => 'int',
          'unsigned' => FALSE,
        ],
        'created' => array(
          'description' => 'A timestamp of when this entry has been created.',
          'type' => 'int',
        ),
      ],
    ];

    return $columns;
  }

In the widget I set correcty the value but the created date must to be always the current time(), Can I set this value inside the FieldItem definition? Witch method have I to use?

Drupal como paradigma.