--- /dev/null
+{% extends "base/class.php.twig" %}
+
+{% block file_path %}
+\Drupal\{{ module }}\Plugin\Field\FieldType\{{ class_name }}.
+{% endblock %}
+
+{% block namespace_class %}
+namespace Drupal\{{ module }}\Plugin\Field\FieldType;
+{% endblock %}
+
+{% block use_class %}
+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\StringTranslation\TranslatableMarkup;
+use Drupal\Core\TypedData\DataDefinition;
+{% endblock %}
+
+{% block class_declaration %}
+/**
+ * Plugin implementation of the '{{ plugin_id }}' field type.
+ *
+ * @FieldType(
+ * id = "{{ plugin_id }}",
+ * label = @Translation("{{ label }}"),
+ * description = @Translation("{{ description }}"){% if default_widget or default_formatter %},
+{% endif %}
+{% if default_widget %}
+ * default_widget = "{{ default_widget }}"{% if default_widget and default_formatter %},
+{% endif %}
+{% else %}
+{% endif %}
+{% if default_formatter %}
+ * default_formatter = "{{ default_formatter }}"
+{% else %}
+{% endif %}
+ * )
+ */
+class {{ class_name }} extends FieldItemBase {% endblock %}
+{% block class_methods %}
+ /**
+ * {@inheritdoc}
+ */
+ public static function defaultStorageSettings() {
+ return [
+ 'max_length' => 255,
+ 'is_ascii' => FALSE,
+ 'case_sensitive' => FALSE,
+ ] + parent::defaultStorageSettings();
+ }
+
+ /**
+ * {@inheritdoc}
+ */
+ public static function propertyDefinitions(FieldStorageDefinitionInterface $field_definition) {
+ // Prevent early t() calls by using the TranslatableMarkup.
+ $properties['value'] = DataDefinition::create('string')
+ ->setLabel(new TranslatableMarkup('Text value'))
+ ->setSetting('case_sensitive', $field_definition->getSetting('case_sensitive'))
+ ->setRequired(TRUE);
+
+ return $properties;
+ }
+
+ /**
+ * {@inheritdoc}
+ */
+ public static function schema(FieldStorageDefinitionInterface $field_definition) {
+ $schema = [
+ 'columns' => [
+ 'value' => [
+ 'type' => $field_definition->getSetting('is_ascii') === TRUE ? 'varchar_ascii' : 'varchar',
+ 'length' => (int) $field_definition->getSetting('max_length'),
+ 'binary' => $field_definition->getSetting('case_sensitive'),
+ ],
+ ],
+ ];
+
+ return $schema;
+ }
+
+ /**
+ * {@inheritdoc}
+ */
+ public function getConstraints() {
+ $constraints = parent::getConstraints();
+
+ if ($max_length = $this->getSetting('max_length')) {
+ $constraint_manager = \Drupal::typedDataManager()->getValidationConstraintManager();
+ $constraints[] = $constraint_manager->create('ComplexData', [
+ 'value' => [
+ 'Length' => [
+ 'max' => $max_length,
+ 'maxMessage' => t('%name: may not be longer than @max characters.', [
+ '%name' => $this->getFieldDefinition()->getLabel(),
+ '@max' => $max_length
+ ]),
+ ],
+ ],
+ ]);
+ }
+
+ return $constraints;
+ }
+
+ /**
+ * {@inheritdoc}
+ */
+ public static function generateSampleValue(FieldDefinitionInterface $field_definition) {
+ $random = new Random();
+ $values['value'] = $random->word(mt_rand(1, $field_definition->getSetting('max_length')));
+ return $values;
+ }
+
+ /**
+ * {@inheritdoc}
+ */
+ public function storageSettingsForm(array &$form, FormStateInterface $form_state, $has_data) {
+ $elements = [];
+
+ $elements['max_length'] = [
+ '#type' => 'number',
+ '#title' => t('Maximum length'),
+ '#default_value' => $this->getSetting('max_length'),
+ '#required' => TRUE,
+ '#description' => t('The maximum length of the field in characters.'),
+ '#min' => 1,
+ '#disabled' => $has_data,
+ ];
+
+ return $elements;
+ }
+
+ /**
+ * {@inheritdoc}
+ */
+ public function isEmpty() {
+ $value = $this->get('value')->getValue();
+ return $value === NULL || $value === '';
+ }
+{% endblock %}