5 * Contains \Drupal\Console\Command\Generate\PluginBlockCommand.
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 Symfony\Component\Console\Command\Command;
14 use Drupal\Console\Generator\PluginBlockGenerator;
15 use Drupal\Console\Command\Shared\ServicesTrait;
16 use Drupal\Console\Command\Shared\ModuleTrait;
17 use Drupal\Console\Command\Shared\FormTrait;
18 use Drupal\Console\Command\Shared\ConfirmationTrait;
19 use Drupal\Console\Core\Command\Shared\ContainerAwareCommandTrait;
20 use Drupal\Console\Extension\Manager;
21 use Drupal\Console\Utils\Validator;
22 use Drupal\Console\Core\Utils\StringConverter;
23 use Drupal\Console\Core\Style\DrupalStyle;
24 use Drupal\Console\Core\Utils\ChainQueue;
25 use Drupal\Core\Config\ConfigFactory;
26 use Drupal\Core\Entity\EntityTypeManagerInterface;
27 use Drupal\Core\Render\ElementInfoManagerInterface;
29 class PluginBlockCommand extends Command
34 use ConfirmationTrait;
35 use ContainerAwareCommandTrait;
40 protected $configFactory;
45 protected $chainQueue;
48 * @var PluginBlockGenerator
53 * @var EntityTypeManagerInterface
55 protected $entityTypeManager;
60 protected $extensionManager;
68 * @var StringConverter
70 protected $stringConverter;
73 * @var ElementInfoManagerInterface
75 protected $elementInfoManager;
78 * PluginBlockCommand constructor.
80 * @param ConfigFactory $configFactory
81 * @param ChainQueue $chainQueue
82 * @param PluginBlockGenerator $generator
83 * @param EntityTypeManagerInterface $entityTypeManager
84 * @param Manager $extensionManager
85 * @param Validator $validator
86 * @param StringConverter $stringConverter
87 * @param ElementInfoManagerInterface $elementInfoManager
89 public function __construct(
90 ConfigFactory $configFactory,
91 ChainQueue $chainQueue,
92 PluginBlockGenerator $generator,
93 EntityTypeManagerInterface $entityTypeManager,
94 Manager $extensionManager,
96 StringConverter $stringConverter,
97 ElementInfoManagerInterface $elementInfoManager
99 $this->configFactory = $configFactory;
100 $this->chainQueue = $chainQueue;
101 $this->generator = $generator;
102 $this->entityTypeManager = $entityTypeManager;
103 $this->extensionManager = $extensionManager;
104 $this->validator = $validator;
105 $this->stringConverter = $stringConverter;
106 $this->elementInfoManager = $elementInfoManager;
107 parent::__construct();
110 protected function configure()
113 ->setName('generate:plugin:block')
114 ->setDescription($this->trans('commands.generate.plugin.block.description'))
115 ->setHelp($this->trans('commands.generate.plugin.block.help'))
116 ->addOption('module', null, InputOption::VALUE_REQUIRED, $this->trans('commands.common.options.module'))
120 InputOption::VALUE_OPTIONAL,
121 $this->trans('commands.generate.plugin.block.options.class')
126 InputOption::VALUE_OPTIONAL,
127 $this->trans('commands.generate.plugin.block.options.label')
132 InputOption::VALUE_OPTIONAL,
133 $this->trans('commands.generate.plugin.block.options.plugin-id')
138 InputOption::VALUE_OPTIONAL,
139 $this->trans('commands.generate.plugin.block.options.theme-region')
144 InputOption::VALUE_OPTIONAL | InputOption::VALUE_IS_ARRAY,
145 $this->trans('commands.common.options.inputs')
150 InputOption::VALUE_OPTIONAL | InputOption::VALUE_IS_ARRAY,
151 $this->trans('commands.common.options.services')
158 protected function execute(InputInterface $input, OutputInterface $output)
160 $io = new DrupalStyle($input, $output);
162 // @see use Drupal\Console\Command\Shared\ConfirmationTrait::confirmGeneration
163 if (!$this->confirmGeneration($io)) {
167 $module = $input->getOption('module');
168 $class_name = $input->getOption('class');
169 $label = $input->getOption('label');
170 $plugin_id = $input->getOption('plugin-id');
171 $services = $input->getOption('services');
172 $theme_region = $input->getOption('theme-region');
173 $inputs = $input->getOption('inputs');
175 $theme = $this->configFactory->get('system.theme')->get('default');
176 $themeRegions = \system_region_list($theme, REGIONS_VISIBLE);
178 if (!empty($theme_region) && !isset($themeRegions[$theme_region])) {
181 $this->trans('commands.generate.plugin.block.messages.invalid-theme-region'),
189 // @see use Drupal\Console\Command\Shared\ServicesTrait::buildServices
190 $build_services = $this->buildServices($services);
202 $this->chainQueue->addCommand('cache:rebuild', ['cache' => 'discovery']);
205 $block = $this->entityTypeManager
206 ->getStorage('block')
210 'plugin' => $plugin_id,
214 $block->setRegion($theme_region);
219 protected function interact(InputInterface $input, OutputInterface $output)
221 $io = new DrupalStyle($input, $output);
223 $theme = $this->configFactory->get('system.theme')->get('default');
224 $themeRegions = \system_region_list($theme, REGIONS_VISIBLE);
227 $module = $input->getOption('module');
229 // @see Drupal\Console\Command\Shared\ModuleTrait::moduleQuestion
230 $module = $this->moduleQuestion($io);
231 $input->setOption('module', $module);
235 $class = $input->getOption('class');
238 $this->trans('commands.generate.plugin.block.questions.class'),
241 return $this->validator->validateClassName($class);
244 $input->setOption('class', $class);
248 $label = $input->getOption('label');
251 $this->trans('commands.generate.plugin.block.questions.label'),
252 $this->stringConverter->camelCaseToHuman($class)
254 $input->setOption('label', $label);
257 // --plugin-id option
258 $pluginId = $input->getOption('plugin-id');
260 $pluginId = $io->ask(
261 $this->trans('commands.generate.plugin.block.questions.plugin-id'),
262 $this->stringConverter->camelCaseToUnderscore($class)
264 $input->setOption('plugin-id', $pluginId);
267 // --theme-region option
268 $themeRegion = $input->getOption('theme-region');
270 $themeRegion = $io->choiceNoList(
271 $this->trans('commands.generate.plugin.block.questions.theme-region'),
272 array_values($themeRegions),
276 $themeRegion = array_search($themeRegion, $themeRegions);
277 $input->setOption('theme-region', $themeRegion);
281 // @see Drupal\Console\Command\Shared\ServicesTrait::servicesQuestion
282 $services = $this->servicesQuestion($io);
283 $input->setOption('services', $services);
285 $output->writeln($this->trans('commands.generate.plugin.block.messages.inputs'));
287 // @see Drupal\Console\Command\Shared\FormTrait::formQuestion
288 $inputs = $this->formQuestion($io);
289 $input->setOption('inputs', $inputs);