|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * @file |
| 5 | + * Contains \Drupal\Console\Command\Generate\BlockTypeCommand. |
| 6 | + */ |
| 7 | + |
| 8 | +namespace Drupal\Console\Command\Generate; |
| 9 | + |
| 10 | +use Drupal\Console\Command\Shared\ArrayInputTrait; |
| 11 | +use Drupal\Console\Command\Shared\FormTrait; |
| 12 | +use Drupal\Console\Command\Shared\ConfirmationTrait; |
| 13 | +use Drupal\Console\Command\Shared\ModuleTrait; |
| 14 | +use Drupal\Console\Command\Shared\ServicesTrait; |
| 15 | +use Drupal\Console\Core\Command\ContainerAwareCommand; |
| 16 | +use Drupal\Console\Core\Utils\StringConverter; |
| 17 | +use Drupal\Console\Core\Utils\ChainQueue; |
| 18 | +use Drupal\Console\Generator\BlockTypeGenerator; |
| 19 | +use Drupal\Console\Extension\Manager; |
| 20 | +use Drupal\Console\Utils\Validator; |
| 21 | +use Drupal\Core\Config\ConfigFactory; |
| 22 | +use Drupal\Core\Entity\EntityTypeManagerInterface; |
| 23 | +use Drupal\Core\Render\ElementInfoManagerInterface; |
| 24 | +use Symfony\Component\Console\Input\InputInterface; |
| 25 | +use Symfony\Component\Console\Input\InputOption; |
| 26 | +use Symfony\Component\Console\Output\OutputInterface; |
| 27 | +use Drupal\block_content\Entity\BlockContent; |
| 28 | +use Drupal\block\Entity\Block; |
| 29 | +use Drupal\block_content\BlockContentTypeInterface; |
| 30 | +use Drupal\block_content\Entity\BlockContentType; |
| 31 | + |
| 32 | +class BlockTypeCommand extends ContainerAwareCommand |
| 33 | +{ |
| 34 | + use ArrayInputTrait; |
| 35 | + use ServicesTrait; |
| 36 | + use ModuleTrait; |
| 37 | + use FormTrait; |
| 38 | + use ConfirmationTrait; |
| 39 | + |
| 40 | + /** |
| 41 | + * @var ConfigFactory |
| 42 | + */ |
| 43 | + protected $configFactory; |
| 44 | + |
| 45 | + /** |
| 46 | + * @var ChainQueue |
| 47 | + */ |
| 48 | + protected $chainQueue; |
| 49 | + |
| 50 | + /** |
| 51 | + * @var BlockTypeGenerator |
| 52 | + */ |
| 53 | + protected $generator; |
| 54 | + |
| 55 | + /** |
| 56 | + * @var EntityTypeManagerInterface |
| 57 | + */ |
| 58 | + protected $entityTypeManager; |
| 59 | + |
| 60 | + /** |
| 61 | + * @var Manager |
| 62 | + */ |
| 63 | + protected $extensionManager; |
| 64 | + |
| 65 | + /** |
| 66 | + * @var Validator |
| 67 | + */ |
| 68 | + protected $validator; |
| 69 | + |
| 70 | + /** |
| 71 | + * @var StringConverter |
| 72 | + */ |
| 73 | + protected $stringConverter; |
| 74 | + |
| 75 | + /** |
| 76 | + * @var ElementInfoManagerInterface |
| 77 | + */ |
| 78 | + protected $elementInfoManager; |
| 79 | + |
| 80 | + /** |
| 81 | + * BlockTypeCommand constructor. |
| 82 | + * |
| 83 | + * @param ConfigFactory $configFactory |
| 84 | + * @param ChainQueue $chainQueue |
| 85 | + * @param BlockTypeGenerator $generator |
| 86 | + * @param EntityTypeManagerInterface $entityTypeManager |
| 87 | + * @param Manager $extensionManager |
| 88 | + * @param Validator $validator |
| 89 | + * @param StringConverter $stringConverter |
| 90 | + * @param ElementInfoManagerInterface $elementInfoManager |
| 91 | + */ |
| 92 | + public function __construct( |
| 93 | + ConfigFactory $configFactory, |
| 94 | + ChainQueue $chainQueue, |
| 95 | + BlockTypeGenerator $generator, |
| 96 | + EntityTypeManagerInterface $entityTypeManager, |
| 97 | + Manager $extensionManager, |
| 98 | + Validator $validator, |
| 99 | + StringConverter $stringConverter, |
| 100 | + ElementInfoManagerInterface $elementInfoManager |
| 101 | + ) { |
| 102 | + $this->configFactory = $configFactory; |
| 103 | + $this->chainQueue = $chainQueue; |
| 104 | + $this->generator = $generator; |
| 105 | + $this->entityTypeManager = $entityTypeManager; |
| 106 | + $this->extensionManager = $extensionManager; |
| 107 | + $this->validator = $validator; |
| 108 | + $this->stringConverter = $stringConverter; |
| 109 | + $this->elementInfoManager = $elementInfoManager; |
| 110 | + parent::__construct(); |
| 111 | + } |
| 112 | + |
| 113 | + protected function configure() |
| 114 | + { |
| 115 | + $this |
| 116 | + ->setName('generate:block:type') |
| 117 | + ->setDescription($this->trans('commands.generate.block.type.description')) |
| 118 | + ->setHelp($this->trans('commands.generate.block.type.help')) |
| 119 | + ->addOption( |
| 120 | + 'module', |
| 121 | + null, |
| 122 | + InputOption::VALUE_REQUIRED, |
| 123 | + $this->trans('commands.common.options.module') |
| 124 | + ) |
| 125 | + ->addOption( |
| 126 | + 'class', |
| 127 | + null, |
| 128 | + InputOption::VALUE_OPTIONAL, |
| 129 | + $this->trans('commands.generate.block.type.options.class') |
| 130 | + ) |
| 131 | + ->addOption( |
| 132 | + 'block-label', |
| 133 | + null, |
| 134 | + InputOption::VALUE_OPTIONAL, |
| 135 | + $this->trans('commands.generate.block.type.options.block-label') |
| 136 | + ) |
| 137 | + ->addOption( |
| 138 | + 'block-description', |
| 139 | + null, |
| 140 | + InputOption::VALUE_OPTIONAL, |
| 141 | + $this->trans('commands.generate.block.type.options.block-description') |
| 142 | + ) |
| 143 | + ->addOption( |
| 144 | + 'block-id', |
| 145 | + null, |
| 146 | + InputOption::VALUE_OPTIONAL, |
| 147 | + $this->trans('commands.generate.block.type.options.block-id') |
| 148 | + ) |
| 149 | + |
| 150 | + ->setAliases(['gbt']); |
| 151 | + } |
| 152 | + |
| 153 | + /** |
| 154 | + * {@inheritdoc} |
| 155 | + */ |
| 156 | + protected function execute(InputInterface $input, OutputInterface $output) |
| 157 | + { |
| 158 | + // @see use Drupal\Console\Command\Shared\ConfirmationTrait::confirmOperation |
| 159 | + if (!$this->confirmOperation()) { |
| 160 | + return 1; |
| 161 | + } |
| 162 | + |
| 163 | + $module = $this->validateModule($input->getOption('module')); |
| 164 | + $class_name = $this->validator->validateClassName($input->getOption('class')); |
| 165 | + $block_label = $input->getOption('block-label'); |
| 166 | + $block_description = $input->getOption('block-description'); |
| 167 | + $block_id = $input->getOption('block-id'); |
| 168 | + |
| 169 | + $theme_region = true; |
| 170 | + |
| 171 | + $this->generator->generate([ |
| 172 | + 'module' => $module, |
| 173 | + 'class_name' => $class_name, |
| 174 | + 'label' => $block_label, |
| 175 | + 'description' => $block_description, |
| 176 | + 'block_id' => $block_id, |
| 177 | + ]); |
| 178 | + |
| 179 | + $this->chainQueue->addCommand('cache:rebuild', ['cache' => 'discovery']); |
| 180 | + |
| 181 | + if ($theme_region) { |
| 182 | + $block_content_type = BlockContentType::create([ |
| 183 | + 'id' => $block_id, |
| 184 | + 'label' => $block_label, |
| 185 | + 'description' => $block_description, |
| 186 | + |
| 187 | + ]); |
| 188 | + $block_content_type->save(); |
| 189 | + |
| 190 | + $block_content = BlockContent::create([ |
| 191 | + 'info' => $block_label, |
| 192 | + 'type' => $block_id, |
| 193 | + 'body' => [ |
| 194 | + 'value' => "<h1>Block's body</h1>", |
| 195 | + 'format' => 'full_html', |
| 196 | + ], |
| 197 | + ]); |
| 198 | + |
| 199 | + $block_content->save(); |
| 200 | + } |
| 201 | + } |
| 202 | + |
| 203 | + protected function interact(InputInterface $input, OutputInterface $output) |
| 204 | + { |
| 205 | + // --module option |
| 206 | + $this->getModuleOption(); |
| 207 | + |
| 208 | + // --class option |
| 209 | + $class = $input->getOption('class'); |
| 210 | + if (!$class) { |
| 211 | + $class = $this->getIo()->ask( |
| 212 | + $this->trans('commands.generate.block.type.questions.class'), |
| 213 | + 'DefaultBlockContentType', |
| 214 | + function ($class) { |
| 215 | + return $this->validator->validateClassName($class); |
| 216 | + } |
| 217 | + ); |
| 218 | + $input->setOption('class', $class); |
| 219 | + } |
| 220 | + |
| 221 | + // --block-label option |
| 222 | + $block_label = $input->getOption('block-label'); |
| 223 | + if (!$block_label) { |
| 224 | + $block_label = $this->getIo()->ask( |
| 225 | + $this->trans('commands.generate.block.type.questions.block-label'), |
| 226 | + $this->stringConverter->camelCaseToHuman($class) |
| 227 | + ); |
| 228 | + $input->setOption('block-label', $block_label); |
| 229 | + } |
| 230 | + |
| 231 | + // --block-id option |
| 232 | + $blockId = $input->getOption('block-id'); |
| 233 | + if (!$blockId) { |
| 234 | + $blockId = $this->getIo()->ask( |
| 235 | + $this->trans('commands.generate.block.type.questions.block-id'), |
| 236 | + $this->stringConverter->camelCaseToUnderscore($class) |
| 237 | + ); |
| 238 | + $input->setOption('block-id', $blockId); |
| 239 | + } |
| 240 | + // --block-description option |
| 241 | + $blockDesc = $input->getOption('block-description'); |
| 242 | + if (!$blockDesc) { |
| 243 | + $blockDesc = $this->getIo()->ask( |
| 244 | + $this->trans('commands.generate.block.type.questions.block-description'), |
| 245 | + $this->stringConverter->camelCaseToUnderscore($class) |
| 246 | + ); |
| 247 | + $input->setOption('block-description', $blockDesc); |
| 248 | + } |
| 249 | + } |
| 250 | +} |
0 commit comments