Field.php
8.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
<?php
declare(strict_types=1);
namespace DrupalCodeGenerator\Command;
use DrupalCodeGenerator\Application;
use DrupalCodeGenerator\Asset\Assets;
use DrupalCodeGenerator\Attribute\Generator;
use DrupalCodeGenerator\GeneratorType;
use DrupalCodeGenerator\Utils;
use DrupalCodeGenerator\Validator\Required;
use DrupalCodeGenerator\Validator\RequiredMachineName;
#[Generator(
name: 'field',
description: 'Generates a field',
templatePath: Application::TEMPLATE_PATH . '/_field',
type: GeneratorType::MODULE_COMPONENT,
)]
final class Field extends BaseGenerator {
/**
* Field sub-types.
*/
private const array SUB_TYPES = [
'boolean' => [
'label' => 'Boolean',
'list' => FALSE,
'random' => FALSE,
'inline' => FALSE,
'link' => FALSE,
'data_type' => 'boolean',
],
'string' => [
'label' => 'Text',
'list' => TRUE,
'random' => TRUE,
'inline' => TRUE,
'link' => FALSE,
'data_type' => 'string',
],
'text' => [
'label' => 'Text (long)',
'list' => FALSE,
'random' => TRUE,
'inline' => FALSE,
'link' => FALSE,
'data_type' => 'string',
],
'integer' => [
'label' => 'Integer',
'list' => TRUE,
'random' => FALSE,
'inline' => TRUE,
'link' => FALSE,
'data_type' => 'integer',
],
'float' => [
'label' => 'Float',
'list' => TRUE,
'random' => FALSE,
'inline' => TRUE,
'link' => FALSE,
'data_type' => 'float',
],
'numeric' => [
'label' => 'Numeric',
'list' => TRUE,
'random' => FALSE,
'inline' => TRUE,
'link' => FALSE,
'data_type' => 'float',
],
'email' => [
'label' => 'Email',
'list' => TRUE,
'random' => TRUE,
'inline' => TRUE,
'link' => TRUE,
'data_type' => 'email',
],
'telephone' => [
'label' => 'Telephone',
'list' => TRUE,
'random' => FALSE,
'inline' => TRUE,
'link' => TRUE,
'data_type' => 'string',
],
'uri' => [
'label' => 'Url',
'list' => TRUE,
'random' => TRUE,
'inline' => TRUE,
'link' => TRUE,
'data_type' => 'uri',
],
'datetime' => [
'label' => 'Date',
'list' => TRUE,
'random' => FALSE,
'inline' => FALSE,
'link' => FALSE,
'data_type' => 'datetime_iso8601',
],
];
/**
* Date types.
*/
private const array DATE_TYPES = [
'date' => 'Date only',
'datetime' => 'Date and time',
];
/**
* {@inheritdoc}
*/
protected function generate(array &$vars, Assets $assets): void {
$ir = $this->createInterviewer($vars);
$vars['machine_name'] = $ir->askMachineName();
$vars['name'] = $ir->askName();
$vars['field_label'] = $ir->ask('Field label', 'Example', new Required());
$vars['field_id'] = $ir->ask('Field ID', '{machine_name}_{field_label|h2m}', new RequiredMachineName());
$subfield_count_validator = static function (mixed $value): int {
if (!(\is_int($value) || \ctype_digit($value)) || (int) $value <= 0) {
throw new \UnexpectedValueException('The value should be greater than zero.');
}
return (int) $value;
};
$vars['subfield_count'] = $ir->ask('How many sub-fields would you like to create?', '3', $subfield_count_validator);
$type_choices = \array_combine(
\array_keys(self::SUB_TYPES),
\array_column(self::SUB_TYPES, 'label'),
);
// Indicates that at least one of sub-fields needs Random component.
$vars['random'] = FALSE;
// Indicates that all sub-fields can be rendered inline.
$vars['inline'] = TRUE;
// Indicates that at least one of sub-fields has limited allowed values.
$vars['list'] = FALSE;
// Indicates that at least one of sub-fields is required.
$vars['required'] = FALSE;
// Indicates that at least one of sub-fields is of email type.
$vars['email'] = FALSE;
// Indicates that at least one of sub-fields can be rendered as a link.
$vars['link'] = FALSE;
// Indicates that at least one of sub-fields is of datetime type.
$vars['datetime'] = FALSE;
$vars['type_class'] = '{field_label|camelize}Item';
$vars['widget_class'] = '{field_label|camelize}Widget';
$vars['formatter_class'] = '{field_label|camelize}DefaultFormatter';
for ($i = 1; $i <= $vars['subfield_count']; $i++) {
$this->io()->writeln(\sprintf('<fg=green>%s</>', \str_repeat('–', 50)));
$subfield = new \stdClass();
$subfield->name = $ir->ask("Label for sub-field #$i", "Value $i");
$subfield->machineName = $ir->ask(
"Machine name for sub-field #$i",
Utils::human2machine($subfield->name),
new RequiredMachineName(),
);
/** @var string $type */
$type = $ir->choice("Type of sub-field #$i", $type_choices, 'Text');
$subfield->dateType = $type === 'datetime' ?
$ir->choice("Date type for sub-field #$i", self::DATE_TYPES, 'Date only') : NULL;
$definition = self::SUB_TYPES[$type];
if ($definition['list']) {
$subfield->list = $ir->confirm("Limit allowed values for sub-field #$i?", FALSE);
}
$subfield->required = $ir->confirm("Make sub-field #$i required?", FALSE);
// Build sub-field vars.
$vars['subfields'][$i] = [
'name' => $subfield->name,
'machine_name' => $subfield->machineName,
'type' => $type,
'data_type' => $definition['data_type'],
'list' => $subfield->list ?? FALSE,
'allowed_values_method' => 'allowed' . Utils::camelize($subfield->name, TRUE) . 'Values',
'required' => $subfield->required,
'link' => $definition['link'],
];
if ($subfield->dateType) {
$vars['subfields'][$i]['date_type'] = $subfield->dateType;
// Back to date type ID.
$vars['subfields'][$i]['date_storage_format'] = $subfield->dateType === 'date' ? 'Y-m-d' : 'Y-m-d\TH:i:s';
}
if ($definition['random']) {
$vars['random'] = TRUE;
}
if (!$definition['inline']) {
$vars['inline'] = FALSE;
}
if ($vars['subfields'][$i]['list']) {
$vars['list'] = TRUE;
}
if ($vars['subfields'][$i]['required']) {
$vars['required'] = TRUE;
}
if ($type === 'email') {
$vars['email'] = TRUE;
}
if ($definition['link']) {
$vars['link'] = TRUE;
}
if ($type === 'datetime') {
$vars['datetime'] = TRUE;
}
}
$this->io()->writeln(\sprintf('<fg=green>%s</>', \str_repeat('–', 50)));
$vars['storage_settings'] = $ir->confirm('Would you like to create field storage settings form?', FALSE);
$vars['instance_settings'] = $ir->confirm('Would you like to create field instance settings form?', FALSE);
$vars['widget_settings'] = $ir->confirm('Would you like to create field widget settings form?', FALSE);
$vars['formatter_settings'] = $ir->confirm('Would you like to create field formatter settings form?', FALSE);
$vars['table_formatter'] = $ir->confirm('Would you like to create table formatter?', FALSE);
$vars['key_value_formatter'] = $ir->confirm('Would you like to create key-value formatter?', FALSE);
$assets->addFile('src/Plugin/Field/FieldType/{type_class}.php', 'type.twig');
$assets->addFile('src/Plugin/Field/FieldWidget/{widget_class}.php', 'widget.twig');
$assets->addFile('src/Plugin/Field/FieldFormatter/{formatter_class}.php', 'default-formatter.twig');
$assets->addSchemaFile()->template('schema.twig');
$assets->addFile('{machine_name}.libraries.yml', 'libraries.twig')
->appendIfExists();
$assets->addFile('css/{field_id|u2h}-widget.css', 'widget-css.twig');
if ($vars['table_formatter']) {
$vars['table_formatter_class'] = '{field_label|camelize}TableFormatter';
$assets->addFile('src/Plugin/Field/FieldFormatter/{table_formatter_class}.php', '/table-formatter.twig');
}
if ($vars['key_value_formatter']) {
$vars['key_value_formatter_class'] = '{field_label|camelize}KeyValueFormatter';
$assets->addFile('src/Plugin/Field/FieldFormatter/{key_value_formatter_class}.php', 'key-value-formatter.twig');
}
}
}