-
-
Notifications
You must be signed in to change notification settings - Fork 555
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[generate:plugin:derivative] New command (#4173)
* create command [generate:plugin:derivative] * fix constructor * fix name template * Fix annotation
- Loading branch information
1 parent
0ae799f
commit ddb53e5
Showing
6 changed files
with
456 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,221 @@ | ||
<?php | ||
|
||
/** | ||
* @file | ||
* Contains \Drupal\Console\Command\Generate\PluginDerivativeCommand. | ||
*/ | ||
|
||
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\Extension\Manager; | ||
use Drupal\Console\Utils\Validator; | ||
use Drupal\Core\Config\ConfigFactory; | ||
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\Console\Generator\PluginDerivativeGenerator; | ||
|
||
class PluginDerivativeCommand extends ContainerAwareCommand | ||
{ | ||
use ArrayInputTrait; | ||
use ServicesTrait; | ||
use ModuleTrait; | ||
use FormTrait; | ||
use ConfirmationTrait; | ||
|
||
/** | ||
* @var ConfigFactory | ||
*/ | ||
protected $configFactory; | ||
|
||
/** | ||
* @var ChainQueue | ||
*/ | ||
protected $chainQueue; | ||
|
||
/** | ||
* @var PluginDerivativeGenerator | ||
*/ | ||
protected $generator; | ||
|
||
/** | ||
* @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 PluginDerivativeGenerator $generator | ||
* @param Manager $extensionManager | ||
* @param Validator $validator | ||
* @param StringConverter $stringConverter | ||
* @param ElementInfoManagerInterface $elementInfoManager | ||
*/ | ||
public function __construct( | ||
ConfigFactory $configFactory, | ||
ChainQueue $chainQueue, | ||
PluginDerivativeGenerator $generator, | ||
Manager $extensionManager, | ||
Validator $validator, | ||
StringConverter $stringConverter | ||
) { | ||
$this->configFactory = $configFactory; | ||
$this->chainQueue = $chainQueue; | ||
$this->generator = $generator; | ||
$this->extensionManager = $extensionManager; | ||
$this->validator = $validator; | ||
$this->stringConverter = $stringConverter; | ||
parent::__construct(); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
protected function configure() | ||
{ | ||
$this | ||
->setName('generate:plugin:derivative') | ||
->setDescription($this->trans('commands.generate.derivative.description')) | ||
->setHelp($this->trans('commands.generate.plugin.derivative.help')) | ||
->addOption( | ||
'module', | ||
null, | ||
InputOption::VALUE_REQUIRED, | ||
$this->trans('commands.common.options.module') | ||
) | ||
->addOption( | ||
'class', | ||
null, | ||
InputOption::VALUE_OPTIONAL, | ||
$this->trans('commands.generate.plugin.derivative.options.class') | ||
) | ||
->addOption( | ||
'block_label', | ||
null, | ||
InputOption::VALUE_OPTIONAL, | ||
$this->trans('commands.generate.plugin.derivative.options.block_label') | ||
) | ||
->addOption( | ||
'block_description', | ||
null, | ||
InputOption::VALUE_OPTIONAL, | ||
$this->trans('commands.generate.plugin.derivative.options.block_description') | ||
) | ||
->addOption( | ||
'block_id', | ||
null, | ||
InputOption::VALUE_OPTIONAL, | ||
$this->trans('commands.generate.plugin.derivative.options.block_id') | ||
) | ||
->setAliases(['gpd']); | ||
} | ||
|
||
/** | ||
* {@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' => $class_name, | ||
'block_label' => $block_label, | ||
'block_description' => $block_description, | ||
'block_id' => $block_id, | ||
] | ||
); | ||
|
||
$this->chainQueue->addCommand('cache:rebuild', ['cache' => 'discovery']); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
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.plugin.derivative.questions.class'), | ||
$this->trans('commands.generate.plugin.derivative.suggestions.class'), | ||
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.plugin.derivative.questions.block_label'), | ||
$this->trans('commands.generate.plugin.derivative.suggestions.block_label') | ||
); | ||
$input->setOption('block_label', $block_label); | ||
} | ||
|
||
// --block_id option | ||
$blockId = $input->getOption('block_id'); | ||
if (!$blockId) { | ||
$blockId = $this->getIo()->ask( | ||
$this->trans('commands.generate.plugin.derivative.questions.block_id'), | ||
$this->stringConverter->camelCaseToUnderscore($blockId) | ||
); | ||
$input->setOption('block_id', $blockId); | ||
} | ||
// --block_description option | ||
$blockDesc = $input->getOption('block_description'); | ||
if (!$blockDesc) { | ||
$blockDesc = $this->getIo()->ask( | ||
$this->trans('commands.generate.plugin.derivative.questions.block_description'), | ||
$this->trans('commands.generate.plugin.derivative.suggestions.block_description') | ||
); | ||
$input->setOption('block_description', $blockDesc); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
<?php | ||
|
||
/** | ||
* @file | ||
* Contains \Drupal\Console\Generator\PluginDerivativeGenerator. | ||
*/ | ||
|
||
namespace Drupal\Console\Generator; | ||
|
||
use Drupal\Console\Core\Generator\Generator; | ||
use Drupal\Console\Extension\Manager; | ||
|
||
class PluginDerivativeGenerator 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']; | ||
$blockLabel = $parameters['block_label']; | ||
$blockDescription = $parameters['block_description']; | ||
$blockId = $parameters['block_id']; | ||
|
||
//block_derivative.php.twig | ||
$this->renderFile( | ||
'module/src/Plugin/Block/block_derivative.php.twig', | ||
$this->extensionManager->getPluginPath($module, 'Block') . '/' . $class_name . '.php', | ||
$parameters | ||
); | ||
|
||
//derivative_block_derivative.php.twig | ||
$this->renderFile( | ||
'module/src/Plugin/Derivative/derivative_block_derivative.php.twig', | ||
$this->extensionManager->getPluginPath($module, 'Derivative') . '/' . $class_name . '.php', | ||
$parameters | ||
); | ||
|
||
} | ||
} |
93 changes: 93 additions & 0 deletions
93
templates/module/src/Plugin/Block/block_derivative.php.twig
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
{% extends "base/class.php.twig" %} | ||
|
||
{% block file_path %} | ||
\Drupal\{{module}}\Plugin\Block\{{class}}. | ||
{% endblock %} | ||
|
||
{% block namespace_class %} | ||
namespace Drupal\{{module}}\Plugin\Block; | ||
{% endblock %} | ||
|
||
{% block use_class %} | ||
use Drupal\block\BlockBase; | ||
use Drupal\Core\Entity\EntityManagerInterface; | ||
use Drupal\Core\Entity\EntityViewBuilderInterface; | ||
use Drupal\Core\Plugin\ContainerFactoryPluginInterface; | ||
use Drupal\Core\Session\AccountInterface; | ||
use Drupal\node\NodeInterface; | ||
use Symfony\Component\DependencyInjection\ContainerInterface; | ||
{% endblock %} | ||
|
||
{% block class_declaration %} | ||
/** | ||
* Provides a '{{class}}' block. | ||
* | ||
* @Block( | ||
* id = "{{block_id}}", | ||
* admin_label = @Translation("{{block_label}}"), | ||
* category = @Translation("{{block_description}}"), | ||
* deriver = "Drupal\{{module}}\Plugin\Derivative\{{class}}" | ||
* ) | ||
*/ | ||
|
||
class {{class}} extends BlockBase | ||
{% endblock %} | ||
{% block class_properties %} | ||
/** | ||
* @var EntityViewBuilderInterface. | ||
*/ | ||
private $_viewBuilder; | ||
|
||
/** | ||
* @var NodeInterface. | ||
*/ | ||
private $_node; | ||
{% endblock %} | ||
{% block class_construct %} | ||
|
||
/** | ||
* Creates a {{class}} instance. | ||
* | ||
* @param array $configuration | ||
* @param string $plugin_id | ||
* @param mixed $plugin_definition | ||
* @param EntityManagerInterface $entity_manager | ||
*/ | ||
public function __construct(array $configuration, $plugin_id, $plugin_definition, EntityManagerInterface $entity_manager) | ||
{ | ||
parent::__construct($configuration, $plugin_id, $plugin_definition); | ||
$this->viewBuilder = $entity_manager->getViewBuilder('node'); | ||
$this->nodeStorage = $entity_manager->getStorage('node'); | ||
$this->node = $entity_manager->getStorage('node')->load($this->getDerivativeId()); | ||
} | ||
{% endblock %} | ||
{% block class_create %} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) | ||
{ | ||
return new static( | ||
$configuration, | ||
$plugin_id, | ||
$plugin_definition, | ||
$container->get('entity.manager') | ||
); | ||
} | ||
|
||
{% endblock %} | ||
{% block class_methods %} | ||
/** | ||
* {@inheritdoc} | ||
*/ | ||
|
||
public function build() | ||
{ | ||
if (!$this->node instanceof NodeInterface) { | ||
return; | ||
} | ||
$build = $this->viewBuilder->view($this->node, 'full'); | ||
return $build; | ||
} | ||
{% endblock %} |
Oops, something went wrong.