Pull merge.
[yaffs-website] / vendor / chi-teck / drupal-code-generator / templates / d8 / _field / type.twig
1 <?php
2
3 namespace Drupal\{{ machine_name }}\Plugin\Field\FieldType;
4
5 {% if random %}
6 use Drupal\Component\Utility\Random;
7 {% endif %}
8 use Drupal\Core\Field\FieldDefinitionInterface;
9 use Drupal\Core\Field\FieldItemBase;
10 use Drupal\Core\Field\FieldStorageDefinitionInterface;
11 {% if storage_settings or instance_settings %}
12 use Drupal\Core\Form\FormStateInterface;
13 {% endif %}
14 {% if email %}
15 use Drupal\Core\Render\Element\Email;
16 {% endif %}
17 use Drupal\Core\TypedData\DataDefinition;
18
19 /**
20  * Defines the '{{ field_id }}' field type.
21  *
22  * @FieldType(
23  *   id = "{{ field_id }}",
24  *   label = @Translation("{{ field_label }}"),
25  *   category = @Translation("General"),
26  *   default_widget = "{{ field_id }}",
27  *   default_formatter = "{{ field_id }}_default"
28  * )
29  */
30 class {{ type_class }} extends FieldItemBase {
31
32 {% if storage_settings %}
33   /**
34    * {@inheritdoc}
35    */
36   public static function defaultStorageSettings() {
37     $settings = ['foo' => 'example'];
38     return $settings + parent::defaultStorageSettings();
39   }
40
41   /**
42    * {@inheritdoc}
43    */
44   public function storageSettingsForm(array &$form, FormStateInterface $form_state, $has_data) {
45     $settings = $this->getSettings();
46
47     $element['foo'] = [
48       '#type' => 'textfield',
49       '#title' => $this->t('Foo'),
50       '#default_value' => $settings['foo'],
51       '#disabled' => $has_data,
52     ];
53
54     return $element;
55   }
56
57 {% endif %}
58 {% if instance_settings %}
59   /**
60    * {@inheritdoc}
61    */
62   public static function defaultFieldSettings() {
63     $settings = ['bar' => 'example'];
64     return $settings + parent::defaultFieldSettings();
65   }
66
67   /**
68    * {@inheritdoc}
69    */
70   public function fieldSettingsForm(array $form, FormStateInterface $form_state) {
71     $settings = $this->getSettings();
72
73     $element['bar'] = [
74       '#type' => 'textfield',
75       '#title' => $this->t('Foo'),
76       '#default_value' => $settings['bar'],
77     ];
78
79     return $element;
80   }
81
82 {% endif %}
83   /**
84    * {@inheritdoc}
85    */
86   public function isEmpty() {
87 {% for subfield in subfields %}
88   {% set condition %}
89     {% if subfield.type == 'boolean' %}$this->{{ subfield.machine_name }} == 1{% else %}$this->{{ subfield.machine_name }} !== NULL{% endif %}
90   {% endset %}
91   {% if loop.index == 1 %}
92     if ({{ condition }}) {
93   {% else %}
94     elseif ({{ condition }}) {
95   {% endif %}
96       return FALSE;
97     }
98 {% endfor %}
99     return TRUE;
100   }
101
102   /**
103    * {@inheritdoc}
104    */
105   public static function propertyDefinitions(FieldStorageDefinitionInterface $field_definition) {
106
107 {% for subfield in subfields %}
108     $properties['{{ subfield.machine_name }}'] = DataDefinition::create('{{ subfield.data_type }}')
109       ->setLabel(t('{{ subfield.name }}'));
110 {% endfor %}
111
112     return $properties;
113   }
114
115   /**
116    * {@inheritdoc}
117    */
118   public function getConstraints() {
119     $constraints = parent::getConstraints();
120
121 {% for subfield in subfields %}
122   {% if subfield.list %}
123     $options['{{ subfield.machine_name }}']['AllowedValues'] = array_keys({{ type_class }}::{{ subfield.allowed_values_method }}());
124
125   {% endif %}
126   {% if subfield.required %}
127     {% if subfield.type == 'boolean' %}
128     // NotBlank validator is not suitable for booleans because it does not
129     // recognize '0' as an empty value.
130     $options['{{ subfield.machine_name }}']['AllowedValues']['choices'] = [1];
131     $options['{{ subfield.machine_name }}']['AllowedValues']['message'] = $this->t('This value should not be blank.');
132
133     {% else %}
134     $options['{{ subfield.machine_name }}']['NotBlank'] = [];
135
136       {% if subfield.type == 'email' %}
137     $options['{{ subfield.machine_name }}']['Length']['max'] = Email::EMAIL_MAX_LENGTH;
138
139       {% endif %}
140     {% endif %}
141   {% endif %}
142 {% endfor %}
143 {% if list or required %}
144     $constraint_manager = \Drupal::typedDataManager()->getValidationConstraintManager();
145     $constraints[] = $constraint_manager->create('ComplexData', $options);
146 {% endif %}
147     // @todo Add more constrains here.
148     return $constraints;
149   }
150
151   /**
152    * {@inheritdoc}
153    */
154   public static function schema(FieldStorageDefinitionInterface $field_definition) {
155
156     $columns = [
157 {% for subfield in subfields %}
158       '{{ subfield.machine_name }}' => [
159   {% if subfield.type == 'boolean' %}
160         'type' => 'int',
161         'size' => 'tiny',
162   {% elseif subfield.type == 'string' %}
163         'type' => 'varchar',
164         'length' => 255,
165   {% elseif subfield.type == 'text' %}
166         'type' => 'text',
167         'size' => 'big',
168   {% elseif subfield.type == 'integer' %}
169         'type' => 'int',
170         'size' => 'normal',
171   {% elseif subfield.type == 'float' %}
172         'type' => 'float',
173         'size' => 'normal',
174   {% elseif subfield.type == 'numeric' %}
175         'type' => 'numeric',
176         'precision' => 10,
177         'scale' => 2,
178   {% elseif subfield.type == 'email' %}
179         'type' => 'varchar',
180         'length' => Email::EMAIL_MAX_LENGTH,
181   {% elseif subfield.type == 'telephone' %}
182         'type' => 'varchar',
183         'length' => 255,
184   {% elseif subfield.type == 'uri' %}
185         'type' => 'varchar',
186         'length' => 2048,
187   {% elseif subfield.type == 'datetime' %}
188         'type' => 'varchar',
189         'length' => 20,
190   {% endif %}
191       ],
192 {% endfor %}
193     ];
194
195     $schema = [
196       'columns' => $columns,
197       // @DCG Add indexes here if necessary.
198     ];
199
200     return $schema;
201   }
202
203   /**
204    * {@inheritdoc}
205    */
206   public static function generateSampleValue(FieldDefinitionInterface $field_definition) {
207
208 {% if random %}
209     $random = new Random();
210
211 {% endif %}
212 {% for subfield in subfields %}
213   {% if subfield.list %}
214     $values['{{ subfield.machine_name }}'] = array_rand(self::{{ subfield.allowed_values_method }}());
215
216   {% elseif subfield.type == 'boolean' %}
217     $values['{{ subfield.machine_name }}'] = (bool) mt_rand(0, 1);
218
219   {% elseif subfield.type == 'string' %}
220     $values['{{ subfield.machine_name }}'] = $random->word(mt_rand(1, 255));
221
222   {% elseif subfield.type == 'text' %}
223     $values['{{ subfield.machine_name }}'] = $random->paragraphs(5);
224
225   {% elseif subfield.type == 'integer' %}
226     $values['{{ subfield.machine_name }}'] = mt_rand(-1000, 1000);
227
228   {% elseif subfield.type == 'float' %}
229     $scale = rand(1, 5);
230     $random_decimal = mt_rand() / mt_getrandmax() * (1000 - 0);
231     $values['{{ subfield.machine_name }}'] = floor($random_decimal * pow(10, $scale)) / pow(10, $scale);
232
233   {% elseif subfield.type == 'numeric' %}
234     $scale = rand(10, 2);
235     $random_decimal = -1000 + mt_rand() / mt_getrandmax() * (-1000 - 1000);
236     $values['{{ subfield.machine_name }}'] = floor($random_decimal * pow(10, $scale)) / pow(10, $scale);
237
238   {% elseif subfield.type == 'email' %}
239     $values['{{ subfield.machine_name }}'] = strtolower($random->name()) . '@example.com';
240
241   {% elseif subfield.type == 'telephone' %}
242     $values['{{ subfield.machine_name }}'] = mt_rand(pow(10, 8), pow(10, 9) - 1);
243
244   {% elseif subfield.type == 'uri' %}
245     $tlds = ['com', 'net', 'gov', 'org', 'edu', 'biz', 'info'];
246     $domain_length = mt_rand(7, 15);
247     $protocol = mt_rand(0, 1) ? 'https' : 'http';
248     $www = mt_rand(0, 1) ? 'www' : '';
249     $domain = $random->word($domain_length);
250     $tld = $tlds[mt_rand(0, (count($tlds) - 1))];
251     $values['{{ subfield.machine_name }}'] = "$protocol://$www.$domain.$tld";
252
253   {% elseif subfield.type == 'datetime' %}
254     $timestamp = \Drupal::time()->getRequestTime() - mt_rand(0, 86400 * 365);
255     $values['{{ subfield.machine_name }}'] = gmdate('{{ subfield.date_storage_format }}', $timestamp);
256
257   {% endif %}
258 {% endfor %}
259     return $values;
260   }
261
262 {% for subfield in subfields %}
263   {% if subfield.list %}
264   /**
265    * Returns allowed values for '{{ subfield.machine_name }}' sub-field.
266    *
267    * @return array
268    *   The list of allowed values.
269    */
270   public static function {{ subfield.allowed_values_method }}() {
271     return [
272     {% if subfield.type == 'string' %}
273       'alpha' => t('Alpha'),
274       'beta' => t('Beta'),
275       'gamma' => t('Gamma'),
276     {% elseif subfield.type == 'integer' %}
277       123 => 123,
278       456 => 456,
279       789 => 789,
280     {% elseif subfield.type == 'float' %}
281       '12.3' => '12.3',
282       '4.56' => '4.56',
283       '0.789' => '0.789',
284     {% elseif subfield.type == 'numeric' %}
285       '12.35' => '12.35',
286       '45.65' => '45.65',
287       '78.95' => '78.95',
288     {% elseif subfield.type == 'email' %}
289       'alpha@example.com' => 'alpha@example.com',
290       'beta@example.com' => 'beta@example.com',
291       'gamma@example.com' => 'gamma@example.com',
292     {% elseif subfield.type == 'telephone' %}
293       '71234567001' => '+7(123)45-67-001',
294       '71234567002' => '+7(123)45-67-002',
295       '71234567003' => '+7(123)45-67-003',
296     {% elseif subfield.type == 'uri' %}
297       'https://example.com' => 'https://example.com',
298       'http://www.php.net' => 'http://www.php.net',
299       'https://www.drupal.org' => 'https://www.drupal.org',
300     {% elseif subfield.type == 'datetime' %}
301       {% if subfield.date_type == 'date' %}
302       '2018-01-01' => '1 January 2018',
303       '2018-02-01' => '1 February 2018',
304       '2018-03-01' => '1 March 2018',
305       {% else %}
306       '2018-01-01T00:10:10' => '1 January 2018, 00:10:10',
307       '2018-02-01T00:20:20' => '1 February 2018, 00:20:20',
308       '2018-03-01T00:30:30' => '1 March 2018, 00:30:30',
309       {% endif %}
310     {% endif %}
311     ];
312   }
313
314   {% endif %}
315 {% endfor %}
316 }