--- /dev/null
+<?php
+
+namespace Drupal\{{ machine_name }}\Plugin\Field\FieldType;
+
+use Drupal\Component\Utility\Random;
+use Drupal\Core\Field\FieldDefinitionInterface;
+use Drupal\Core\Field\FieldItemBase;
+use Drupal\Core\Field\FieldStorageDefinitionInterface;
+use Drupal\Core\Form\FormStateInterface;
+use Drupal\Core\TypedData\DataDefinition;
+
+/**
+ * Defines the '{{ plugin_id }}' field type.
+ *
+ * @FieldType(
+ * id = "{{ plugin_id }}",
+ * label = @Translation("{{ plugin_label }}"),
+ * category = @Translation("General"),
+ * default_widget = "string_textfield",
+ * default_formatter = "string"
+ * )
+ *
+ * @DCG
+ * If you are implementing a single value field type you may want to inherit
+ * this class form some of the field type classes provided by Drupal core.
+ * Check out /core/lib/Drupal/Core/Field/Plugin/Field/FieldType directory for
+ * list of available field type implementations.
+ */
+class {{ class }} extends FieldItemBase {
+
+ /**
+ * {@inheritdoc}
+ */
+ public static function defaultStorageSettings() {
+ $settings = ['foo' => 123];
+ return $settings + parent::defaultStorageSettings();
+ }
+
+ /**
+ * {@inheritdoc}
+ */
+ public function storageSettingsForm(array &$form, FormStateInterface $form_state, $has_data) {
+ $settings = $this->getSettings();
+
+ $element['foo'] = [
+ '#type' => 'number',
+ '#title' => $this->t('Foo'),
+ '#default_value' => $settings['foo'],
+ '#disabled' => $has_data,
+ ];
+
+ return $element;
+ }
+
+ /**
+ * {@inheritdoc}
+ */
+ public static function defaultFieldSettings() {
+ $settings = ['bar' => 'Bla bla bla'];
+ return $settings + parent::defaultFieldSettings();
+ }
+
+ /**
+ * {@inheritdoc}
+ */
+ public function fieldSettingsForm(array $form, FormStateInterface $form_state) {
+ $settings = $this->getSettings();
+
+ $element['bar'] = [
+ '#type' => 'textfield',
+ '#title' => t('Bar'),
+ '#default_value' => $settings['bar'],
+ ];
+
+ return $element;
+ }
+
+ /**
+ * {@inheritdoc}
+ */
+ public function isEmpty() {
+ $value = $this->get('value')->getValue();
+ return $value === NULL || $value === '';
+ }
+
+ /**
+ * {@inheritdoc}
+ */
+ public static function propertyDefinitions(FieldStorageDefinitionInterface $field_definition) {
+
+ // @DCG
+ // See /core/lib/Drupal/Core/TypedData/Plugin/DataType directory for
+ // available data types.
+ $properties['value'] = DataDefinition::create('string')
+ ->setLabel(t('Text value'))
+ ->setRequired(TRUE);
+
+ return $properties;
+ }
+
+ /**
+ * {@inheritdoc}
+ */
+ public function getConstraints() {
+ $constraints = parent::getConstraints();
+
+ $constraint_manager = \Drupal::typedDataManager()->getValidationConstraintManager();
+
+ // @DCG Suppose our value must not be longer than 10 characters.
+ $options['value']['Length']['max'] = 10;
+
+ // @DCG
+ // See /core/lib/Drupal/Core/Validation/Plugin/Validation/Constraint
+ // directory for available constraints.
+ $constraints[] = $constraint_manager->create('ComplexData', $options);
+ return $constraints;
+ }
+
+ /**
+ * {@inheritdoc}
+ */
+ public static function schema(FieldStorageDefinitionInterface $field_definition) {
+
+ $columns = [
+ 'value' => [
+ 'type' => 'varchar',
+ 'not null' => FALSE,
+ 'description' => 'Column description.',
+ 'length' => 255,
+ ],
+ ];
+
+ $schema = [
+ 'columns' => $columns,
+ // @DCG Add indexes here if needed.
+ ];
+
+ return $schema;
+ }
+
+ /**
+ * {@inheritdoc}
+ */
+ public static function generateSampleValue(FieldDefinitionInterface $field_definition) {
+ $random = new Random();
+ $values['value'] = $random->word(mt_rand(1, 50));
+ return $values;
+ }
+
+}