5 * Contains Drupal\Console\Command\Shared\FormTrait.
8 namespace Drupal\Console\Command\Shared;
10 use Drupal\Console\Core\Style\DrupalStyle;
15 * @package Drupal\Console\Command
20 * @param DrupalStyle $io
24 public function formQuestion(DrupalStyle $io)
27 $this->trans('commands.common.questions.inputs.confirm'),
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;
50 $input_type = $io->choiceNoList(
51 $this->trans('commands.common.questions.inputs.type'),
57 if (empty($input_type)) {
62 $inputLabelMessage = $input_type == 'fieldset'?$this->trans('commands.common.questions.inputs.title'):$this->trans('commands.common.questions.inputs.label');
63 $input_label = $io->ask(
69 $input_machine_name = $this->stringConverter->createMachineName($input_label);
71 $input_name = $io->ask(
72 $this->trans('commands.common.questions.inputs.machine_name'),
76 if ($input_type == 'fieldset') {
77 $fieldSets[$input_machine_name] = $input_label;
81 if ($input_type != 'fieldset' && !empty($fieldSets)) {
82 $inputFieldSet = $io->choiceNoList(
83 $this->trans('commands.common.questions.inputs.fieldset'),
89 $inputFieldSet = array_search($inputFieldSet, $fieldSets);
94 if (in_array($input_type, ['textfield', 'password', 'password_confirm'])) {
95 $maxlength = $io->ask(
96 'Maximum amount of characters',
101 'Width of the textfield (in characters)',
106 if ($input_type == 'select') {
108 'Size of multiselect box (in lines)',
114 if (in_array($input_type, ['checkboxes', 'radios', 'select'])) {
115 $input_options = $io->ask(
116 'Input options separated by comma'
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."')";
129 $input_options = '['.implode(', ', $input_options_output).']';
132 // Description for input
133 $input_description = $io->askEmpty(
134 $this->trans('commands.common.questions.inputs.description')
137 // Default value for input
138 switch ($input_type) {
140 $question = 'commands.common.questions.inputs.default-value.checkboxes';
143 $question = 'commands.common.questions.inputs.default-value.default-value';
146 if ($input_type != 'fieldset') {
147 $default_value = $io->askEmpty(
148 $this->trans($question)
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;
162 $this->trans('commands.common.questions.inputs.weight'),
169 'name' => $input_name,
170 'type' => $input_type,
171 'label' => $input_label,
172 'options' => $input_options,
173 'description' => $input_description,
174 'maxlength' => $maxlength,
176 'default_value' => $default_value,
178 'fieldset' => $inputFieldSet,