5 * Contains \Drupal\Console\Command\Generate\PluginFieldCommand.
8 namespace Drupal\Console\Command\Generate;
10 use Drupal\Console\Utils\Validator;
11 use Symfony\Component\Console\Input\InputInterface;
12 use Symfony\Component\Console\Input\InputOption;
13 use Symfony\Component\Console\Output\OutputInterface;
14 use Drupal\Console\Command\Shared\ModuleTrait;
15 use Drupal\Console\Command\Shared\ConfirmationTrait;
16 use Drupal\Console\Core\Command\Command;
17 use Drupal\Console\Extension\Manager;
18 use Drupal\Console\Core\Utils\StringConverter;
19 use Drupal\Console\Core\Utils\ChainQueue;
21 class PluginFieldCommand extends Command
24 use ConfirmationTrait;
29 protected $extensionManager;
32 * @var StringConverter
34 protected $stringConverter;
44 protected $chainQueue;
48 * PluginFieldCommand constructor.
50 * @param Manager $extensionManager
51 * @param StringConverter $stringConverter
52 * @param Validator $validator
53 * @param ChainQueue $chainQueue
55 public function __construct(
56 Manager $extensionManager,
57 StringConverter $stringConverter,
59 ChainQueue $chainQueue
61 $this->extensionManager = $extensionManager;
62 $this->stringConverter = $stringConverter;
63 $this->validator = $validator;
64 $this->chainQueue = $chainQueue;
65 parent::__construct();
68 protected function configure()
71 ->setName('generate:plugin:field')
72 ->setDescription($this->trans('commands.generate.plugin.field.description'))
73 ->setHelp($this->trans('commands.generate.plugin.field.help'))
77 InputOption::VALUE_REQUIRED,
78 $this->trans('commands.common.options.module')
83 InputOption::VALUE_REQUIRED,
84 $this->trans('commands.generate.plugin.field.options.type-class')
89 InputOption::VALUE_OPTIONAL,
90 $this->trans('commands.generate.plugin.field.options.type-label')
95 InputOption::VALUE_OPTIONAL,
96 $this->trans('commands.generate.plugin.field.options.type-plugin-id')
101 InputOption::VALUE_OPTIONAL,
102 $this->trans('commands.generate.plugin.field.options.type-description')
107 InputOption::VALUE_REQUIRED,
108 $this->trans('commands.generate.plugin.field.options.formatter-class')
113 InputOption::VALUE_OPTIONAL,
114 $this->trans('commands.generate.plugin.field.options.formatter-label')
117 'formatter-plugin-id',
119 InputOption::VALUE_OPTIONAL,
120 $this->trans('commands.generate.plugin.field.options.formatter-plugin-id')
125 InputOption::VALUE_REQUIRED,
126 $this->trans('commands.generate.plugin.field.options.formatter-class')
131 InputOption::VALUE_OPTIONAL,
132 $this->trans('commands.generate.plugin.field.options.widget-label')
137 InputOption::VALUE_OPTIONAL,
138 $this->trans('commands.generate.plugin.field.options.widget-plugin-id')
143 InputOption::VALUE_OPTIONAL,
144 $this->trans('commands.generate.plugin.field.options.field-type')
149 InputOption::VALUE_OPTIONAL,
150 $this->trans('commands.generate.plugin.field.options.default-widget')
155 InputOption::VALUE_OPTIONAL,
156 $this->trans('commands.generate.plugin.field.options.default-formatter')
158 ->setAliases(['gpf']);
164 protected function execute(InputInterface $input, OutputInterface $output)
166 // @see use Drupal\Console\Command\Shared\ConfirmationTrait::confirmOperation
167 if (!$this->confirmOperation()) {
173 'generate:plugin:fieldtype', [
174 '--module' => $input->getOption('module'),
175 '--class' => $this->validator->validateClassName($input->getOption('type-class')),
176 '--label' => $input->getOption('type-label'),
177 '--plugin-id' => $input->getOption('type-plugin-id'),
178 '--description' => $input->getOption('type-description'),
179 '--default-widget' => $input->getOption('default-widget'),
180 '--default-formatter' => $input->getOption('default-formatter'),
187 'generate:plugin:fieldwidget', [
188 '--module' => $input->getOption('module'),
189 '--class' => $this->validator->validateClassName($input->getOption('widget-class')),
190 '--label' => $input->getOption('widget-label'),
191 '--plugin-id' => $input->getOption('widget-plugin-id'),
192 '--field-type' => $input->getOption('field-type'),
198 'generate:plugin:fieldformatter', [
199 '--module' => $input->getOption('module'),
200 '--class' => $this->validator->validateClassName($input->getOption('formatter-class')),
201 '--label' => $input->getOption('formatter-label'),
202 '--plugin-id' => $input->getOption('formatter-plugin-id'),
203 '--field-type' => $input->getOption('field-type'),
208 $this->chainQueue->addCommand('cache:rebuild', ['cache' => 'discovery'], false);
213 protected function interact(InputInterface $input, OutputInterface $output)
216 $this->getModuleOption();
218 // --type-class option
219 $typeClass = $input->getOption('type-class');
221 $typeClass = $this->getIo()->ask(
222 $this->trans('commands.generate.plugin.field.questions.type-class'),
224 function ($typeClass) {
225 return $this->validator->validateClassName($typeClass);
228 $input->setOption('type-class', $typeClass);
231 // --type-label option
232 $label = $input->getOption('type-label');
234 $label = $this->getIo()->ask(
235 $this->trans('commands.generate.plugin.field.questions.type-label'),
236 $this->stringConverter->camelCaseToHuman($typeClass)
238 $input->setOption('type-label', $label);
241 // --type-plugin-id option
242 $plugin_id = $input->getOption('type-plugin-id');
244 $plugin_id = $this->getIo()->ask(
245 $this->trans('commands.generate.plugin.field.questions.type-plugin-id'),
246 $this->stringConverter->camelCaseToUnderscore($typeClass)
248 $input->setOption('type-plugin-id', $plugin_id);
251 // --type-description option
252 $description = $input->getOption('type-description');
254 $description = $this->getIo()->ask(
255 $this->trans('commands.generate.plugin.field.questions.type-description'),
256 $this->trans('commands.generate.plugin.field.suggestions.my-field-type')
258 $input->setOption('type-description', $description);
261 // --widget-class option
262 $widgetClass = $input->getOption('widget-class');
264 $widgetClass = $this->getIo()->ask(
265 $this->trans('commands.generate.plugin.field.questions.widget-class'),
267 function ($widgetClass) {
268 return $this->validator->validateClassName($widgetClass);
271 $input->setOption('widget-class', $widgetClass);
274 // --widget-label option
275 $widgetLabel = $input->getOption('widget-label');
277 $widgetLabel = $this->getIo()->ask(
278 $this->trans('commands.generate.plugin.field.questions.widget-label'),
279 $this->stringConverter->camelCaseToHuman($widgetClass)
281 $input->setOption('widget-label', $widgetLabel);
284 // --widget-plugin-id option
285 $widget_plugin_id = $input->getOption('widget-plugin-id');
286 if (!$widget_plugin_id) {
287 $widget_plugin_id = $this->getIo()->ask(
288 $this->trans('commands.generate.plugin.field.questions.widget-plugin-id'),
289 $this->stringConverter->camelCaseToUnderscore($widgetClass)
291 $input->setOption('widget-plugin-id', $widget_plugin_id);
294 // --formatter-class option
295 $formatterClass = $input->getOption('formatter-class');
296 if (!$formatterClass) {
297 $formatterClass = $this->getIo()->ask(
298 $this->trans('commands.generate.plugin.field.questions.formatter-class'),
299 'ExampleFormatterType',
300 function ($formatterClass) {
301 return $this->validator->validateClassName($formatterClass);
304 $input->setOption('formatter-class', $formatterClass);
307 // --formatter-label option
308 $formatterLabel = $input->getOption('formatter-label');
309 if (!$formatterLabel) {
310 $formatterLabel = $this->getIo()->ask(
311 $this->trans('commands.generate.plugin.field.questions.formatter-label'),
312 $this->stringConverter->camelCaseToHuman($formatterClass)
314 $input->setOption('formatter-label', $formatterLabel);
317 // --formatter-plugin-id option
318 $formatter_plugin_id = $input->getOption('formatter-plugin-id');
319 if (!$formatter_plugin_id) {
320 $formatter_plugin_id = $this->getIo()->ask(
321 $this->trans('commands.generate.plugin.field.questions.formatter-plugin-id'),
322 $this->stringConverter->camelCaseToUnderscore($formatterClass)
324 $input->setOption('formatter-plugin-id', $formatter_plugin_id);
327 // --field-type option
328 $field_type = $input->getOption('field-type');
330 $field_type = $this->getIo()->ask(
331 $this->trans('commands.generate.plugin.field.questions.field-type'),
334 $input->setOption('field-type', $field_type);
337 // --default-widget option
338 $default_widget = $input->getOption('default-widget');
339 if (!$default_widget) {
340 $default_widget = $this->getIo()->ask(
341 $this->trans('commands.generate.plugin.field.questions.default-widget'),
344 $input->setOption('default-widget', $default_widget);
347 // --default-formatter option
348 $default_formatter = $input->getOption('default-formatter');
349 if (!$default_formatter) {
350 $default_formatter = $this->getIo()->ask(
351 $this->trans('commands.generate.plugin.field.questions.default-formatter'),
354 $input->setOption('default-formatter', $default_formatter);