Yaffs site version 1.1
[yaffs-website] / vendor / drupal / console / src / Command / Shared / FormTrait.php
1 <?php
2
3 /**
4  * @file
5  * Contains Drupal\Console\Command\Shared\FormTrait.
6  */
7
8 namespace Drupal\Console\Command\Shared;
9
10 use Drupal\Console\Core\Style\DrupalStyle;
11
12 /**
13  * Class FormTrait
14  *
15  * @package Drupal\Console\Command
16  */
17 trait FormTrait
18 {
19     /**
20      * @param DrupalStyle $io
21      *
22      * @return mixed
23      */
24     public function formQuestion(DrupalStyle $io)
25     {
26         if ($io->confirm(
27             $this->trans('commands.common.questions.inputs.confirm'),
28             true
29         )
30         ) {
31             $input_types = [
32                 'fieldset',
33                 'text_format'
34             ];
35
36             foreach ($this->elementInfoManager->getDefinitions() as $definition) {
37                 $type = $definition['id'];
38                 $elementInfo = $this->elementInfoManager->getInfo($type);
39                 if (isset($elementInfo['#input']) && $elementInfo['#input']) {
40                     if (!in_array($type, $input_types)) {
41                         $input_types[] = $type;
42                     }
43                 }
44             }
45             sort($input_types);
46
47             $inputs = [];
48             $fieldSets = [];
49             while (true) {
50                 $input_type = $io->choiceNoList(
51                     $this->trans('commands.common.questions.inputs.type'),
52                     $input_types,
53                     null,
54                     true
55                 );
56
57                 if (empty($input_type)) {
58                     break;
59                 }
60
61                 // Label for input
62                 $inputLabelMessage = $input_type == 'fieldset'?$this->trans('commands.common.questions.inputs.title'):$this->trans('commands.common.questions.inputs.label');
63                 $input_label = $io->ask(
64                     $inputLabelMessage,
65                     null
66                 );
67
68                 // Machine name
69                 $input_machine_name = $this->stringConverter->createMachineName($input_label);
70
71                 $input_name = $io->ask(
72                     $this->trans('commands.common.questions.inputs.machine_name'),
73                     $input_machine_name
74                 );
75
76                 if ($input_type == 'fieldset') {
77                     $fieldSets[$input_machine_name] = $input_label;
78                 }
79
80                 $inputFieldSet = '';
81                 if ($input_type != 'fieldset' && !empty($fieldSets)) {
82                     $inputFieldSet = $io->choiceNoList(
83                         $this->trans('commands.common.questions.inputs.fieldset'),
84                         $fieldSets,
85                         null,
86                         true
87                     );
88
89                     $inputFieldSet = array_search($inputFieldSet, $fieldSets);
90                 }
91
92                 $maxlength = null;
93                 $size = null;
94                 if (in_array($input_type, ['textfield', 'password', 'password_confirm'])) {
95                     $maxlength = $io->ask(
96                         'Maximum amount of characters',
97                         '64'
98                     );
99
100                     $size = $io->ask(
101                         'Width of the textfield (in characters)',
102                         '64'
103                     );
104                 }
105
106                 if ($input_type == 'select') {
107                     $size = $io->ask(
108                         'Size of multiselect box (in lines)',
109                         '5'
110                     );
111                 }
112
113                 $input_options = '';
114                 if (in_array($input_type, ['checkboxes', 'radios', 'select'])) {
115                     $input_options = $io->ask(
116                         'Input options separated by comma'
117                     );
118                 }
119
120                 // Prepare options as an array
121                 if (strlen(trim($input_options))) {
122                     // remove spaces in options and empty options
123                     $input_options = array_filter(array_map('trim', explode(',', $input_options)));
124                     // Create array format for options
125                     foreach ($input_options as $key => $value) {
126                         $input_options_output[$key] = "'$value' => \$this->t('".$value."')";
127                     }
128
129                     $input_options = '['.implode(', ', $input_options_output).']';
130                 }
131
132                 // Description for input
133                 $input_description = $io->askEmpty(
134                     $this->trans('commands.common.questions.inputs.description')
135                 );
136
137                 // Default value for input
138                 switch ($input_type) {
139                 case 'checkboxes':
140                     $question = 'commands.common.questions.inputs.default-value.checkboxes';
141                     break;
142                 default:
143                     $question = 'commands.common.questions.inputs.default-value.default-value';
144                     break;
145                 }
146                 if ($input_type != 'fieldset') {
147                     $default_value = $io->askEmpty(
148                         $this->trans($question)
149                     );
150                 }
151                 if ($input_type == 'checkboxes') {
152                     // Prepare options as an array
153                     if (strlen(trim($default_value))) {
154                         // remove spaces in options and empty options
155                         $default_options = array_filter(array_map('trim', explode(',', $default_value)));
156                         $default_value = $default_options;
157                     }
158                 }
159
160                 // Weight for input
161                 $weight = $io->ask(
162                     $this->trans('commands.common.questions.inputs.weight'),
163                     '0'
164                 );
165
166                 array_push(
167                     $inputs,
168                     [
169                         'name' => $input_name,
170                         'type' => $input_type,
171                         'label' => $input_label,
172                         'options' => $input_options,
173                         'description' => $input_description,
174                         'maxlength' => $maxlength,
175                         'size' => $size,
176                         'default_value' => $default_value,
177                         'weight' => $weight,
178                         'fieldset' => $inputFieldSet,
179                     ]
180                 );
181             }
182
183             return $inputs;
184         }
185
186         return null;
187     }
188 }