forked from hechoendrupal/drupal-console
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFormTrait.php
More file actions
125 lines (110 loc) · 4.12 KB
/
Copy pathFormTrait.php
File metadata and controls
125 lines (110 loc) · 4.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
<?php
/**
* @file
* Contains Drupal\AppConsole\Command\Helper\FormTrait.
*/
namespace Drupal\AppConsole\Command\Helper;
use Symfony\Component\Console\Helper\HelperInterface;
use Symfony\Component\Console\Output\OutputInterface;
trait FormTrait
{
/**
* @param OutputInterface $output
* @param HelperInterface $dialog
* @return mixed
*/
public function formQuestion(OutputInterface $output, HelperInterface $dialog)
{
if ($dialog->askConfirmation(
$output,
$dialog->getQuestion($this->trans('commands.common.questions.inputs.confirm'), 'yes', '?'),
true
)
) {
$input_types = [
'color',
'checkbox',
'checkboxes',
'date',
'datetime',
'email',
'number',
'range',
'radios',
'select',
'tel',
'textarea',
'textfield',
];
$inputs = [];
while (true) {
// Label for input
$input_label = $dialog->ask(
$output,
$dialog->getQuestion(' ' . $this->trans('commands.common.questions.inputs.label'), '', ':'),
null
);
if (empty($input_label)) {
break;
}
// Machine name
$input_machine_name = $this->getStringUtils()->createMachineName($input_label);
$input_name = $dialog->ask(
$output,
$dialog->getQuestion(' ' . $this->trans('commands.common.questions.inputs.machine_name'),
$input_machine_name, ':'),
$input_machine_name
);
// Type input
$input_type = $dialog->askAndValidate(
$output,
$dialog->getQuestion(' ' . $this->trans('commands.common.questions.inputs.type'), 'textfield', ':'),
function ($input) use ($input_types) {
if (!in_array($input, $input_types)) {
throw new \InvalidArgumentException(
sprintf($this->trans('commands.common.questions.inputs.invalid'), $input)
);
}
return $input;
},
false,
'textfield',
$input_types
);
$input_options = '';
if (in_array($input_type, array('checkboxes', 'radios', 'select'))) {
$input_options = $dialog->ask(
$output,
$dialog->getQuestion(' Input options separated by comma', '', ':'),
null
);
}
// 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] = "\$this->t('" . $value . "') => \$this->t('" . $value . "')";
}
$input_options = "array(" . implode(", ", $input_options_output) . ")";
}
// Description for input
$input_description = $dialog->ask(
$output,
$dialog->getQuestion(' ' . $this->trans('commands.common.questions.inputs.description'), '', ':'),
null
);
array_push($inputs, array(
'name' => $input_name,
'type' => $input_type,
'label' => $input_label,
'options' => $input_options,
'description' => $input_description,
));
}
return $inputs;
}
return null;
}
}