5 * Contains \Drupal\Console\Command\Generate\PluginFieldWidgetCommand.
8 namespace Drupal\Console\Command\Generate;
10 use Symfony\Component\Console\Input\InputInterface;
11 use Symfony\Component\Console\Input\InputOption;
12 use Symfony\Component\Console\Output\OutputInterface;
13 use Drupal\Console\Generator\PluginFieldWidgetGenerator;
14 use Drupal\Console\Command\Shared\ModuleTrait;
15 use Drupal\Console\Command\Shared\ConfirmationTrait;
16 use Symfony\Component\Console\Command\Command;
17 use Drupal\Console\Core\Style\DrupalStyle;
18 use Drupal\Console\Extension\Manager;
19 use Drupal\Console\Core\Command\Shared\CommandTrait;
20 use Drupal\Console\Core\Utils\StringConverter;
21 use Drupal\Console\Core\Utils\ChainQueue;
22 use Drupal\Core\Field\FieldTypePluginManager;
25 * Class PluginFieldWidgetCommand
27 * @package Drupal\Console\Command\Generate
29 class PluginFieldWidgetCommand extends Command
32 use ConfirmationTrait;
38 protected $extensionManager;
41 * @var PluginFieldWidgetGenerator
46 * @var StringConverter
48 protected $stringConverter;
56 * @var FieldTypePluginManager
58 protected $fieldTypePluginManager;
63 protected $chainQueue;
67 * PluginFieldWidgetCommand constructor.
69 * @param Manager $extensionManager
70 * @param PluginFieldWidgetGenerator $generator
71 * @param StringConverter $stringConverter
72 * @param FieldTypePluginManager $fieldTypePluginManager
73 * @param ChainQueue $chainQueue
75 public function __construct(
76 Manager $extensionManager,
77 PluginFieldWidgetGenerator $generator,
78 StringConverter $stringConverter,
79 FieldTypePluginManager $fieldTypePluginManager,
80 ChainQueue $chainQueue
82 $this->extensionManager = $extensionManager;
83 $this->generator = $generator;
84 $this->stringConverter = $stringConverter;
85 $this->fieldTypePluginManager = $fieldTypePluginManager;
86 $this->chainQueue = $chainQueue;
87 parent::__construct();
90 protected function configure()
93 ->setName('generate:plugin:fieldwidget')
94 ->setDescription($this->trans('commands.generate.plugin.fieldwidget.description'))
95 ->setHelp($this->trans('commands.generate.plugin.fieldwidget.help'))
96 ->addOption('module', null, InputOption::VALUE_REQUIRED, $this->trans('commands.common.options.module'))
100 InputOption::VALUE_REQUIRED,
101 $this->trans('commands.generate.plugin.fieldwidget.options.class')
106 InputOption::VALUE_OPTIONAL,
107 $this->trans('commands.generate.plugin.fieldwidget.options.label')
112 InputOption::VALUE_OPTIONAL,
113 $this->trans('commands.generate.plugin.fieldwidget.options.plugin-id')
118 InputOption::VALUE_OPTIONAL,
119 $this->trans('commands.generate.plugin.fieldwidget.options.field-type')
126 protected function execute(InputInterface $input, OutputInterface $output)
128 $io = new DrupalStyle($input, $output);
130 // @see use Drupal\Console\Command\Shared\ConfirmationTrait::confirmGeneration
131 if (!$this->confirmGeneration($io)) {
135 $module = $input->getOption('module');
136 $class_name = $input->getOption('class');
137 $label = $input->getOption('label');
138 $plugin_id = $input->getOption('plugin-id');
139 $field_type = $input->getOption('field-type');
141 $this->generator->generate($module, $class_name, $label, $plugin_id, $field_type);
143 $this->chainQueue->addCommand('cache:rebuild', ['cache' => 'discovery']);
148 protected function interact(InputInterface $input, OutputInterface $output)
150 $io = new DrupalStyle($input, $output);
153 $module = $input->getOption('module');
155 // @see Drupal\Console\Command\Shared\ModuleTrait::moduleQuestion
156 $module = $this->moduleQuestion($io);
157 $input->setOption('module', $module);
161 $class_name = $input->getOption('class');
163 $class_name = $io->ask(
164 $this->trans('commands.generate.plugin.fieldwidget.questions.class'),
167 $input->setOption('class', $class_name);
170 // --plugin label option
171 $label = $input->getOption('label');
174 $this->trans('commands.generate.plugin.fieldwidget.questions.label'),
175 $this->stringConverter->camelCaseToHuman($class_name)
177 $input->setOption('label', $label);
180 // --plugin-id option
181 $plugin_id = $input->getOption('plugin-id');
183 $plugin_id = $io->ask(
184 $this->trans('commands.generate.plugin.fieldwidget.questions.plugin-id'),
185 $this->stringConverter->camelCaseToUnderscore($class_name)
187 $input->setOption('plugin-id', $plugin_id);
190 // --field-type option
191 $field_type = $input->getOption('field-type');
193 // Gather valid field types.
194 $field_type_options = [];
195 foreach ($this->fieldTypePluginManager->getGroupedDefinitions($this->fieldTypePluginManager->getUiDefinitions()) as $category => $field_types) {
196 foreach ($field_types as $name => $field_type) {
197 $field_type_options[] = $name;
201 $field_type = $io->choice(
202 $this->trans('commands.generate.plugin.fieldwidget.questions.field-type'),
206 $input->setOption('field-type', $field_type);