Yaffs site version 1.1
[yaffs-website] / vendor / drupal / console / templates / module / src / Plugin / Field / FieldType / fieldtype.php.twig
1 {% extends "base/class.php.twig" %}
2
3 {% block file_path %}
4 \Drupal\{{ module }}\Plugin\Field\FieldType\{{ class_name }}.
5 {% endblock %}
6
7 {% block namespace_class %}
8 namespace Drupal\{{ module }}\Plugin\Field\FieldType;
9 {% endblock %}
10
11 {% block use_class %}
12 use Drupal\Component\Utility\Random;
13 use Drupal\Core\Field\FieldDefinitionInterface;
14 use Drupal\Core\Field\FieldItemBase;
15 use Drupal\Core\Field\FieldStorageDefinitionInterface;
16 use Drupal\Core\Form\FormStateInterface;
17 use Drupal\Core\StringTranslation\TranslatableMarkup;
18 use Drupal\Core\TypedData\DataDefinition;
19 {% endblock %}
20
21 {% block class_declaration %}
22 /**
23  * Plugin implementation of the '{{ plugin_id }}' field type.
24  *
25  * @FieldType(
26  *   id = "{{ plugin_id }}",
27  *   label = @Translation("{{ label }}"),
28  *   description = @Translation("{{ description }}"){% if default_widget or default_formatter %},
29 {% endif %}
30 {% if default_widget %}
31  *   default_widget = "{{ default_widget }}"{% if default_widget and default_formatter %},
32 {% endif %}
33 {% else %}
34 {% endif %}
35 {%  if default_formatter %}
36  *   default_formatter = "{{ default_formatter }}"
37 {% else %}
38 {%  endif %}
39  * )
40  */
41 class {{ class_name }} extends FieldItemBase {% endblock %}
42 {% block class_methods %}
43   /**
44    * {@inheritdoc}
45    */
46   public static function defaultStorageSettings() {
47     return [
48       'max_length' => 255,
49       'is_ascii' => FALSE,
50       'case_sensitive' => FALSE,
51     ] + parent::defaultStorageSettings();
52   }
53
54   /**
55    * {@inheritdoc}
56    */
57   public static function propertyDefinitions(FieldStorageDefinitionInterface $field_definition) {
58     // Prevent early t() calls by using the TranslatableMarkup.
59     $properties['value'] = DataDefinition::create('string')
60       ->setLabel(new TranslatableMarkup('Text value'))
61       ->setSetting('case_sensitive', $field_definition->getSetting('case_sensitive'))
62       ->setRequired(TRUE);
63
64     return $properties;
65   }
66
67   /**
68    * {@inheritdoc}
69    */
70   public static function schema(FieldStorageDefinitionInterface $field_definition) {
71     $schema = [
72       'columns' => [
73         'value' => [
74           'type' => $field_definition->getSetting('is_ascii') === TRUE ? 'varchar_ascii' : 'varchar',
75           'length' => (int) $field_definition->getSetting('max_length'),
76           'binary' => $field_definition->getSetting('case_sensitive'),
77         ],
78       ],
79     ];
80
81     return $schema;
82   }
83
84   /**
85    * {@inheritdoc}
86    */
87   public function getConstraints() {
88     $constraints = parent::getConstraints();
89
90     if ($max_length = $this->getSetting('max_length')) {
91       $constraint_manager = \Drupal::typedDataManager()->getValidationConstraintManager();
92       $constraints[] = $constraint_manager->create('ComplexData', [
93         'value' => [
94           'Length' => [
95             'max' => $max_length,
96             'maxMessage' => t('%name: may not be longer than @max characters.', [
97               '%name' => $this->getFieldDefinition()->getLabel(),
98               '@max' => $max_length
99             ]),
100           ],
101         ],
102       ]);
103     }
104
105     return $constraints;
106   }
107
108   /**
109    * {@inheritdoc}
110    */
111   public static function generateSampleValue(FieldDefinitionInterface $field_definition) {
112     $random = new Random();
113     $values['value'] = $random->word(mt_rand(1, $field_definition->getSetting('max_length')));
114     return $values;
115   }
116
117   /**
118    * {@inheritdoc}
119    */
120   public function storageSettingsForm(array &$form, FormStateInterface $form_state, $has_data) {
121     $elements = [];
122
123     $elements['max_length'] = [
124       '#type' => 'number',
125       '#title' => t('Maximum length'),
126       '#default_value' => $this->getSetting('max_length'),
127       '#required' => TRUE,
128       '#description' => t('The maximum length of the field in characters.'),
129       '#min' => 1,
130       '#disabled' => $has_data,
131     ];
132
133     return $elements;
134   }
135
136   /**
137    * {@inheritdoc}
138    */
139   public function isEmpty() {
140     $value = $this->get('value')->getValue();
141     return $value === NULL || $value === '';
142   }
143 {% endblock %}