-
-
Notifications
You must be signed in to change notification settings - Fork 547
Expand file tree
/
Copy pathFieldTrait.php
More file actions
176 lines (148 loc) · 4.67 KB
/
FieldTrait.php
File metadata and controls
176 lines (148 loc) · 4.67 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
<?php
/**
* @file
* Contains Drupal\Console\Command\FormTrait.
*/
namespace Drupal\sammy\Command;
use Drupal\Console\Style\DrupalStyle;
use Drupal\Core\Entity\EntityFieldManager;
use Drupal\Core\Field\FieldTypePluginManagerInterface;
/**
* Class Field
* @package Drupal\Console\Command
*/
trait FieldTrait
{
/**
* @param DrupalStyle $io
*
* @return mixed
*/
public function formQuestion(DrupalStyle $io)
{
if ($io->confirm(
$this->trans('commands.common.questions.inputs.confirm'),
true
)) {
$input_types = [
'fieldset',
];
/** @var FieldTypePluginManagerInterface $fieldInfoManager */
$fieldInfoManager = $this->getService('plugin.manager.field.field_type');
if (!$fieldInfoManager) {
return false;
}
// Gather valid field types.
$field_type_options = array();
foreach ($fieldInfoManager->getSortedDefinitions($fieldInfoManager->getUIDefinitions()) as $category => $field_types) {
foreach ($field_types as $name => $field_type) {
$field_type_options[$category][$name] = $field_type->render();
}
}
$inputs = [];
$fieldSets = [];
while (true) {
$input_type = $io->choiceNoList(
$this->trans('commands.common.questions.inputs.type'),
$field_type_options,
null,
true
);
/* if (empty($input_type)) {
break;
}*/
// Label for input
$inputLabelMessage = $input_type == 'fieldset'?$this->trans('commands.common.questions.inputs.title'):$this->trans('commands.common.questions.inputs.label');
$input_label = $io->ask(
$inputLabelMessage,
null
);
// Machine name
$input_machine_name = $this->getStringHelper()->createMachineName($input_label);
$input_name = $io->ask(
$this->trans('commands.common.questions.inputs.machine_name'),
$input_machine_name
);
if ($input_type == 'fieldset') {
$fieldSets[$input_machine_name] = $input_label;
}
$inputFieldSet = '';
if ($input_type != 'fieldset' && !empty($fieldSets)) {
$inputFieldSet = $io->choiceNoList(
$this->trans('commands.common.questions.inputs.fieldset'),
$fieldSets,
null,
true
);
$inputFieldSet = array_search($inputFieldSet, $fieldSets);
}
$maxlength = null;
$size = null;
if (in_array($input_type, array('textfield', 'password', 'password_confirm'))) {
$maxlength = $io->ask(
'Maximum amount of characters',
'64'
);
$size = $io->ask(
'Width of the textfield (in characters)',
'64'
);
}
if ($input_type == 'select') {
$size = $io->askEmpty(
'Size of multiselect box (in lines)',
'5'
);
}
$input_options = '';
if (in_array($input_type, array('checkboxes', 'radios', 'select'))) {
$input_options = $io->ask(
'Input options separated by comma'
);
}
// Prepare options as an array
if (strlen(trim($input_options))) {
// remove spaces in options and empty options
$input_options = array_filter(array_map('trim', explode(',', $input_options)));
// Create array format for options
foreach ($input_options as $key => $value) {
$input_options_output[$key] = "'$value' => \$this->t('".$value."')";
}
$input_options = 'array('.implode(', ', $input_options_output).')';
}
// Description for input
$input_description = $io->askEmpty(
$this->trans('commands.common.questions.inputs.description')
);
if ($input_type != 'fieldset') {
// Default value for input
$default_value = $io->askEmpty(
$this->trans('commands.common.questions.inputs.default-value')
);
}
// Weight for input
$weight = $io->ask(
$this->trans('commands.common.questions.inputs.weight'),
'0'
);
array_push(
$inputs,
[
'name' => $input_name,
'type' => $input_type,
'label' => $input_label,
'options' => $input_options,
'description' => $input_description,
'maxlength' => $maxlength,
'size' => $size,
'default_value' => $default_value,
'weight' => $weight,
'fieldset' => $inputFieldSet,
]
);
}
return $inputs;
}
return null;
}
}