Skip to content

Commit 2a941ff

Browse files
gilbertomangonesenzolutions
authored andcommitted
[generate:block:type] New command (#4160)
* New command Generate Block Content Type and block type * Organize inputs in order to create a block content type
1 parent 464fb82 commit 2a941ff

File tree

5 files changed

+335
-0
lines changed

5 files changed

+335
-0
lines changed

config/services/generate.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,11 @@ services:
202202
arguments: ['@config.factory', '@console.chain_queue', '@console.pluginblock_generator', '@entity_type.manager', '@console.extension_manager', '@console.validator', '@console.string_converter', '@plugin.manager.element_info']
203203
tags:
204204
- { name: drupal.command }
205+
console.generate_blocktype:
206+
class: Drupal\Console\Command\Generate\BlockTypeCommand
207+
arguments: ['@config.factory', '@console.chain_queue', '@console.blocktype_generator', '@entity_type.manager', '@console.extension_manager', '@console.validator', '@console.string_converter', '@plugin.manager.element_info']
208+
tags:
209+
- { name: drupal.command }
205210
console.generate_command:
206211
class: Drupal\Console\Command\Generate\CommandCommand
207212
arguments: ['@console.command_generator', '@console.extension_manager', '@console.validator', '@console.string_converter', '@console.site']

config/services/generator.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,11 @@ services:
196196
arguments: ['@console.extension_manager']
197197
tags:
198198
- { name: drupal.generator }
199+
console.blocktype_generator:
200+
class: Drupal\Console\Generator\BlockTypeGenerator
201+
arguments: ['@console.extension_manager']
202+
tags:
203+
- { name: drupal.generator }
199204
console.command_generator:
200205
class: Drupal\Console\Generator\CommandGenerator
201206
arguments: ['@console.extension_manager', '@console.translator_manager']
Lines changed: 250 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,250 @@
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+
}

src/Generator/BlockTypeGenerator.php

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<?php
2+
3+
/**
4+
* @file
5+
* Contains \Drupal\Console\Generator\BlockTypeGenerator.
6+
*/
7+
8+
namespace Drupal\Console\Generator;
9+
10+
use Drupal\Console\Core\Generator\Generator;
11+
use Drupal\Console\Extension\Manager;
12+
13+
class BlockTypeGenerator extends Generator
14+
{
15+
/**
16+
* @var Manager
17+
*/
18+
protected $extensionManager;
19+
20+
/**
21+
* PermissionGenerator constructor.
22+
*
23+
* @param Manager $extensionManager
24+
*/
25+
public function __construct(
26+
Manager $extensionManager
27+
) {
28+
$this->extensionManager = $extensionManager;
29+
}
30+
31+
/**
32+
* {@inheritdoc}
33+
*/
34+
public function generate(array $parameters)
35+
{
36+
37+
$module = $parameters['module'];
38+
$class_name = $parameters['class_name'];
39+
$blockId = $parameters['block_id'];
40+
$description = $parameters['block_description'];
41+
$parameters['machine_name'] = $blockId;
42+
43+
$this->renderFile(
44+
'module/src/Plugin/Block/blocktype.php.twig',
45+
$this->extensionManager->getPluginPath($module, 'Block') . '/' . $class_name . '.php',
46+
$parameters
47+
);
48+
49+
}
50+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
{% extends "base/class.php.twig" %}
2+
3+
{% block file_path %}
4+
\Drupal\{{module}}\Plugin\Block\{{class_name}}.
5+
{% endblock %}
6+
7+
{% block namespace_class %}
8+
namespace Drupal\{{module}}\Plugin\Block;
9+
{% endblock %}
10+
11+
{% block use_class %}
12+
use Drupal\Core\Block\BlockBase;
13+
{% endblock %}
14+
15+
{% block class_declaration %}
16+
17+
/**
18+
* {@inheritdoc}
19+
*/
20+
public function build() {
21+
$build = [];
22+
$build['{{block_id}}']['#markup'] = 'Implement {{class_name}}.';
23+
return $build;
24+
}
25+
{% endblock %}

0 commit comments

Comments
 (0)