Skip to content

Commit

Permalink
[generate:block:type] New command (#4160)
Browse files Browse the repository at this point in the history
* New command Generate Block Content Type and block type

* Organize inputs in order to create a block content type
  • Loading branch information
gilbertomangones authored and enzolutions committed Sep 27, 2019
1 parent 464fb82 commit 2a941ff
Show file tree
Hide file tree
Showing 5 changed files with 335 additions and 0 deletions.
5 changes: 5 additions & 0 deletions config/services/generate.yml
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,11 @@ services:
arguments: ['@config.factory', '@console.chain_queue', '@console.pluginblock_generator', '@entity_type.manager', '@console.extension_manager', '@console.validator', '@console.string_converter', '@plugin.manager.element_info']
tags:
- { name: drupal.command }
console.generate_blocktype:
class: Drupal\Console\Command\Generate\BlockTypeCommand
arguments: ['@config.factory', '@console.chain_queue', '@console.blocktype_generator', '@entity_type.manager', '@console.extension_manager', '@console.validator', '@console.string_converter', '@plugin.manager.element_info']
tags:
- { name: drupal.command }
console.generate_command:
class: Drupal\Console\Command\Generate\CommandCommand
arguments: ['@console.command_generator', '@console.extension_manager', '@console.validator', '@console.string_converter', '@console.site']
Expand Down
5 changes: 5 additions & 0 deletions config/services/generator.yml
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,11 @@ services:
arguments: ['@console.extension_manager']
tags:
- { name: drupal.generator }
console.blocktype_generator:
class: Drupal\Console\Generator\BlockTypeGenerator
arguments: ['@console.extension_manager']
tags:
- { name: drupal.generator }
console.command_generator:
class: Drupal\Console\Generator\CommandGenerator
arguments: ['@console.extension_manager', '@console.translator_manager']
Expand Down
250 changes: 250 additions & 0 deletions src/Command/Generate/BlockTypeCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,250 @@
<?php

/**
* @file
* Contains \Drupal\Console\Command\Generate\BlockTypeCommand.
*/

namespace Drupal\Console\Command\Generate;

use Drupal\Console\Command\Shared\ArrayInputTrait;
use Drupal\Console\Command\Shared\FormTrait;
use Drupal\Console\Command\Shared\ConfirmationTrait;
use Drupal\Console\Command\Shared\ModuleTrait;
use Drupal\Console\Command\Shared\ServicesTrait;
use Drupal\Console\Core\Command\ContainerAwareCommand;
use Drupal\Console\Core\Utils\StringConverter;
use Drupal\Console\Core\Utils\ChainQueue;
use Drupal\Console\Generator\BlockTypeGenerator;
use Drupal\Console\Extension\Manager;
use Drupal\Console\Utils\Validator;
use Drupal\Core\Config\ConfigFactory;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Render\ElementInfoManagerInterface;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Drupal\block_content\Entity\BlockContent;
use Drupal\block\Entity\Block;
use Drupal\block_content\BlockContentTypeInterface;
use Drupal\block_content\Entity\BlockContentType;

class BlockTypeCommand extends ContainerAwareCommand
{
use ArrayInputTrait;
use ServicesTrait;
use ModuleTrait;
use FormTrait;
use ConfirmationTrait;

/**
* @var ConfigFactory
*/
protected $configFactory;

/**
* @var ChainQueue
*/
protected $chainQueue;

/**
* @var BlockTypeGenerator
*/
protected $generator;

/**
* @var EntityTypeManagerInterface
*/
protected $entityTypeManager;

/**
* @var Manager
*/
protected $extensionManager;

/**
* @var Validator
*/
protected $validator;

/**
* @var StringConverter
*/
protected $stringConverter;

/**
* @var ElementInfoManagerInterface
*/
protected $elementInfoManager;

/**
* BlockTypeCommand constructor.
*
* @param ConfigFactory $configFactory
* @param ChainQueue $chainQueue
* @param BlockTypeGenerator $generator
* @param EntityTypeManagerInterface $entityTypeManager
* @param Manager $extensionManager
* @param Validator $validator
* @param StringConverter $stringConverter
* @param ElementInfoManagerInterface $elementInfoManager
*/
public function __construct(
ConfigFactory $configFactory,
ChainQueue $chainQueue,
BlockTypeGenerator $generator,
EntityTypeManagerInterface $entityTypeManager,
Manager $extensionManager,
Validator $validator,
StringConverter $stringConverter,
ElementInfoManagerInterface $elementInfoManager
) {
$this->configFactory = $configFactory;
$this->chainQueue = $chainQueue;
$this->generator = $generator;
$this->entityTypeManager = $entityTypeManager;
$this->extensionManager = $extensionManager;
$this->validator = $validator;
$this->stringConverter = $stringConverter;
$this->elementInfoManager = $elementInfoManager;
parent::__construct();
}

protected function configure()
{
$this
->setName('generate:block:type')
->setDescription($this->trans('commands.generate.block.type.description'))
->setHelp($this->trans('commands.generate.block.type.help'))
->addOption(
'module',
null,
InputOption::VALUE_REQUIRED,
$this->trans('commands.common.options.module')
)
->addOption(
'class',
null,
InputOption::VALUE_OPTIONAL,
$this->trans('commands.generate.block.type.options.class')
)
->addOption(
'block-label',
null,
InputOption::VALUE_OPTIONAL,
$this->trans('commands.generate.block.type.options.block-label')
)
->addOption(
'block-description',
null,
InputOption::VALUE_OPTIONAL,
$this->trans('commands.generate.block.type.options.block-description')
)
->addOption(
'block-id',
null,
InputOption::VALUE_OPTIONAL,
$this->trans('commands.generate.block.type.options.block-id')
)

->setAliases(['gbt']);
}

/**
* {@inheritdoc}
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
// @see use Drupal\Console\Command\Shared\ConfirmationTrait::confirmOperation
if (!$this->confirmOperation()) {
return 1;
}

$module = $this->validateModule($input->getOption('module'));
$class_name = $this->validator->validateClassName($input->getOption('class'));
$block_label = $input->getOption('block-label');
$block_description = $input->getOption('block-description');
$block_id = $input->getOption('block-id');

$theme_region = true;

$this->generator->generate([
'module' => $module,
'class_name' => $class_name,
'label' => $block_label,
'description' => $block_description,
'block_id' => $block_id,
]);

$this->chainQueue->addCommand('cache:rebuild', ['cache' => 'discovery']);

if ($theme_region) {
$block_content_type = BlockContentType::create([
'id' => $block_id,
'label' => $block_label,
'description' => $block_description,

]);
$block_content_type->save();

$block_content = BlockContent::create([
'info' => $block_label,
'type' => $block_id,
'body' => [
'value' => "<h1>Block's body</h1>",
'format' => 'full_html',
],
]);

$block_content->save();
}
}

protected function interact(InputInterface $input, OutputInterface $output)
{
// --module option
$this->getModuleOption();

// --class option
$class = $input->getOption('class');
if (!$class) {
$class = $this->getIo()->ask(
$this->trans('commands.generate.block.type.questions.class'),
'DefaultBlockContentType',
function ($class) {
return $this->validator->validateClassName($class);
}
);
$input->setOption('class', $class);
}

// --block-label option
$block_label = $input->getOption('block-label');
if (!$block_label) {
$block_label = $this->getIo()->ask(
$this->trans('commands.generate.block.type.questions.block-label'),
$this->stringConverter->camelCaseToHuman($class)
);
$input->setOption('block-label', $block_label);
}

// --block-id option
$blockId = $input->getOption('block-id');
if (!$blockId) {
$blockId = $this->getIo()->ask(
$this->trans('commands.generate.block.type.questions.block-id'),
$this->stringConverter->camelCaseToUnderscore($class)
);
$input->setOption('block-id', $blockId);
}
// --block-description option
$blockDesc = $input->getOption('block-description');
if (!$blockDesc) {
$blockDesc = $this->getIo()->ask(
$this->trans('commands.generate.block.type.questions.block-description'),
$this->stringConverter->camelCaseToUnderscore($class)
);
$input->setOption('block-description', $blockDesc);
}
}
}
50 changes: 50 additions & 0 deletions src/Generator/BlockTypeGenerator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

/**
* @file
* Contains \Drupal\Console\Generator\BlockTypeGenerator.
*/

namespace Drupal\Console\Generator;

use Drupal\Console\Core\Generator\Generator;
use Drupal\Console\Extension\Manager;

class BlockTypeGenerator extends Generator
{
/**
* @var Manager
*/
protected $extensionManager;

/**
* PermissionGenerator constructor.
*
* @param Manager $extensionManager
*/
public function __construct(
Manager $extensionManager
) {
$this->extensionManager = $extensionManager;
}

/**
* {@inheritdoc}
*/
public function generate(array $parameters)
{

$module = $parameters['module'];
$class_name = $parameters['class_name'];
$blockId = $parameters['block_id'];
$description = $parameters['block_description'];
$parameters['machine_name'] = $blockId;

$this->renderFile(
'module/src/Plugin/Block/blocktype.php.twig',
$this->extensionManager->getPluginPath($module, 'Block') . '/' . $class_name . '.php',
$parameters
);

}
}
25 changes: 25 additions & 0 deletions templates/module/src/Plugin/Block/blocktype.php.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{% extends "base/class.php.twig" %}

{% block file_path %}
\Drupal\{{module}}\Plugin\Block\{{class_name}}.
{% endblock %}

{% block namespace_class %}
namespace Drupal\{{module}}\Plugin\Block;
{% endblock %}

{% block use_class %}
use Drupal\Core\Block\BlockBase;
{% endblock %}

{% block class_declaration %}

/**
* {@inheritdoc}
*/
public function build() {
$build = [];
$build['{{block_id}}']['#markup'] = 'Implement {{class_name}}.';
return $build;
}
{% endblock %}

0 comments on commit 2a941ff

Please sign in to comment.