5 * Contains \Drupal\Console\Command\Generate\PluginFieldWidgetCommand.
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\Generator\PluginFieldWidgetGenerator;
15 use Drupal\Console\Command\Shared\ModuleTrait;
16 use Drupal\Console\Command\Shared\ConfirmationTrait;
17 use Drupal\Console\Core\Command\Command;
18 use Drupal\Console\Extension\Manager;
19 use Drupal\Console\Core\Utils\StringConverter;
20 use Drupal\Console\Core\Utils\ChainQueue;
21 use Drupal\Core\Field\FieldTypePluginManager;
24 * Class PluginFieldWidgetCommand
26 * @package Drupal\Console\Command\Generate
28 class PluginFieldWidgetCommand extends Command
31 use ConfirmationTrait;
36 protected $extensionManager;
39 * @var PluginFieldWidgetGenerator
44 * @var StringConverter
46 protected $stringConverter;
54 * @var FieldTypePluginManager
56 protected $fieldTypePluginManager;
61 protected $chainQueue;
65 * PluginFieldWidgetCommand constructor.
67 * @param Manager $extensionManager
68 * @param PluginFieldWidgetGenerator $generator
69 * @param StringConverter $stringConverter
70 * @param Validator $validator
71 * @param FieldTypePluginManager $fieldTypePluginManager
72 * @param ChainQueue $chainQueue
74 public function __construct(
75 Manager $extensionManager,
76 PluginFieldWidgetGenerator $generator,
77 StringConverter $stringConverter,
79 FieldTypePluginManager $fieldTypePluginManager,
80 ChainQueue $chainQueue
82 $this->extensionManager = $extensionManager;
83 $this->generator = $generator;
84 $this->stringConverter = $stringConverter;
85 $this->validator = $validator;
86 $this->fieldTypePluginManager = $fieldTypePluginManager;
87 $this->chainQueue = $chainQueue;
88 parent::__construct();
91 protected function configure()
94 ->setName('generate:plugin:fieldwidget')
95 ->setDescription($this->trans('commands.generate.plugin.fieldwidget.description'))
96 ->setHelp($this->trans('commands.generate.plugin.fieldwidget.help'))
100 InputOption::VALUE_REQUIRED,
101 $this->trans('commands.common.options.module')
106 InputOption::VALUE_REQUIRED,
107 $this->trans('commands.generate.plugin.fieldwidget.options.class')
112 InputOption::VALUE_OPTIONAL,
113 $this->trans('commands.generate.plugin.fieldwidget.options.label')
118 InputOption::VALUE_OPTIONAL,
119 $this->trans('commands.generate.plugin.fieldwidget.options.plugin-id')
124 InputOption::VALUE_OPTIONAL,
125 $this->trans('commands.generate.plugin.fieldwidget.options.field-type')
127 ->setAliases(['gpfw']);
133 protected function execute(InputInterface $input, OutputInterface $output)
135 // @see use Drupal\Console\Command\Shared\ConfirmationTrait::confirmOperation
136 if (!$this->confirmOperation()) {
140 $module = $input->getOption('module');
141 $class_name = $this->validator->validateClassName($input->getOption('class'));
142 $label = $input->getOption('label');
143 $plugin_id = $input->getOption('plugin-id');
144 $field_type = $input->getOption('field-type');
146 $this->generator->generate([
148 'class_name' => $class_name,
150 'plugin_id' => $plugin_id,
151 'field_type' => $field_type,
154 $this->chainQueue->addCommand('cache:rebuild', ['cache' => 'discovery']);
159 protected function interact(InputInterface $input, OutputInterface $output)
162 $this->getModuleOption();
165 $class_name = $input->getOption('class');
167 $class_name = $this->getIo()->ask(
168 $this->trans('commands.generate.plugin.fieldwidget.questions.class'),
169 'ExampleFieldWidget',
170 function ($class_name) {
171 return $this->validator->validateClassName($class_name);
174 $input->setOption('class', $class_name);
177 // --plugin label option
178 $label = $input->getOption('label');
180 $label = $this->getIo()->ask(
181 $this->trans('commands.generate.plugin.fieldwidget.questions.label'),
182 $this->stringConverter->camelCaseToHuman($class_name)
184 $input->setOption('label', $label);
187 // --plugin-id option
188 $plugin_id = $input->getOption('plugin-id');
190 $plugin_id = $this->getIo()->ask(
191 $this->trans('commands.generate.plugin.fieldwidget.questions.plugin-id'),
192 $this->stringConverter->camelCaseToUnderscore($class_name)
194 $input->setOption('plugin-id', $plugin_id);
197 // --field-type option
198 $field_type = $input->getOption('field-type');
200 // Gather valid field types.
201 $field_type_options = [];
202 foreach ($this->fieldTypePluginManager->getGroupedDefinitions($this->fieldTypePluginManager->getUiDefinitions()) as $category => $field_types) {
203 foreach ($field_types as $name => $field_type) {
204 $field_type_options[] = $name;
208 $field_type = $this->getIo()->choice(
209 $this->trans('commands.generate.plugin.fieldwidget.questions.field-type'),
213 $input->setOption('field-type', $field_type);