Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
A new command : Generates Validation constraint plugin (#3768)
* A new command : Generates Validation constraint plugin * Add a line in the end of the file
- Loading branch information
Showing
7 changed files
with
395 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
221 changes: 221 additions & 0 deletions
221
src/Command/Generate/PluginValidationConstraintCommand.php
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\PluginValidationConstraintCommand. | ||
*/ | ||
|
||
namespace Drupal\Console\Command\Generate; | ||
|
||
use Drupal\Console\Utils\Validator; | ||
use Symfony\Component\Console\Input\InputInterface; | ||
use Symfony\Component\Console\Input\InputOption; | ||
use Symfony\Component\Console\Output\OutputInterface; | ||
use Drupal\Console\Generator\PluginValidationConstraintGenerator; | ||
use Drupal\Console\Command\Shared\ModuleTrait; | ||
use Drupal\Console\Command\Shared\ConfirmationTrait; | ||
use Drupal\Console\Core\Command\Command; | ||
use Drupal\Console\Extension\Manager; | ||
use Drupal\Console\Core\Utils\StringConverter; | ||
use Drupal\Console\Core\Utils\ChainQueue; | ||
|
||
/** | ||
* Class PluginValidationConstraintCommand | ||
* | ||
* @package Drupal\Console\Command\Generate | ||
*/ | ||
class PluginValidationConstraintCommand extends Command | ||
{ | ||
use ModuleTrait; | ||
use ConfirmationTrait; | ||
|
||
/** | ||
* @var Manager | ||
*/ | ||
protected $extensionManager; | ||
|
||
/** | ||
* @var PluginValidationConstraintGenerator | ||
*/ | ||
protected $generator; | ||
|
||
/** | ||
* @var StringConverter | ||
*/ | ||
protected $stringConverter; | ||
|
||
/** | ||
* @var Validator | ||
*/ | ||
protected $validator; | ||
|
||
/** | ||
* @var ChainQueue | ||
*/ | ||
protected $chainQueue; | ||
|
||
|
||
/** | ||
* PluginValidationConstraintCommand constructor. | ||
* | ||
* @param Manager $extensionManager | ||
* @param PluginValidationConstraintGenerator $generator | ||
* @param StringConverter $stringConverter | ||
* @param Validator $validator | ||
* @param ChainQueue $chainQueue | ||
*/ | ||
public function __construct( | ||
Manager $extensionManager, | ||
PluginValidationConstraintGenerator $generator, | ||
StringConverter $stringConverter, | ||
Validator $validator, | ||
ChainQueue $chainQueue | ||
) { | ||
$this->extensionManager = $extensionManager; | ||
$this->generator = $generator; | ||
$this->stringConverter = $stringConverter; | ||
$this->validator = $validator; | ||
$this->chainQueue = $chainQueue; | ||
parent::__construct(); | ||
} | ||
|
||
protected function configure() | ||
{ | ||
$this | ||
->setName('generate:plugin:validationconstraint') | ||
->setDescription($this->trans('commands.generate.plugin.validationconstraint.description')) | ||
->setHelp($this->trans('commands.generate.plugin.validationconstraint.help')) | ||
->addOption( | ||
'module', | ||
null, | ||
InputOption::VALUE_REQUIRED, | ||
$this->trans('commands.common.options.module') | ||
) | ||
->addOption( | ||
'class', | ||
null, | ||
InputOption::VALUE_REQUIRED, | ||
$this->trans('commands.generate.plugin.validationconstraint.options.class') | ||
) | ||
->addOption( | ||
'label', | ||
null, | ||
InputOption::VALUE_OPTIONAL, | ||
$this->trans('commands.generate.plugin.validationconstraint.options.label') | ||
) | ||
->addOption( | ||
'plugin-id', | ||
null, | ||
InputOption::VALUE_OPTIONAL, | ||
$this->trans('commands.generate.plugin.validationconstraint.options.plugin-id') | ||
) | ||
->addOption( | ||
'hook', | ||
null, | ||
InputOption::VALUE_OPTIONAL, | ||
$this->trans('commands.generate.plugin.validationconstraint.options.hook') | ||
) | ||
->addOption( | ||
'field-id', | ||
null, | ||
InputOption::VALUE_OPTIONAL, | ||
$this->trans('commands.generate.plugin.validationconstraint.options.field-id') | ||
) | ||
->addOption( | ||
'bundle', | ||
null, | ||
InputOption::VALUE_OPTIONAL, | ||
$this->trans('commands.generate.plugin.validationconstraint.options.bundle') | ||
) | ||
->setAliases(['gpvc']); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
protected function execute(InputInterface $input, OutputInterface $output) | ||
{ | ||
// @see use Drupal\Console\Command\Shared\ConfirmationTrait::confirmOperation | ||
if (!$this->confirmOperation()) { | ||
return 1; | ||
} | ||
|
||
$module = $input->getOption('module'); | ||
$className = $this->validator->validateClassName($input->getOption('class')); | ||
$label = $input->getOption('label'); | ||
$pluginId = $input->getOption('plugin-id'); | ||
$hook = $input->getOption('hook'); | ||
$fieldId = $input->getOption('field-id'); | ||
$bundle = $input->getOption('bundle'); | ||
|
||
$this->generator->generate([ | ||
'module' => $module, | ||
'class_name' => $className, | ||
'label' => $label, | ||
'plugin_id' => $pluginId, | ||
'field_id' => $fieldId, | ||
'hook' => $hook, | ||
'bundle' => $bundle, | ||
]); | ||
|
||
$this->chainQueue->addCommand('cache:rebuild', ['cache' => 'discovery']); | ||
|
||
return 0; | ||
} | ||
|
||
protected function interact(InputInterface $input, OutputInterface $output) | ||
{ | ||
// --module option | ||
$this->getModuleOption(); | ||
|
||
// --class option | ||
$className = $input->getOption('class'); | ||
if (!$className) { | ||
$className = $this->getIo()->ask( | ||
$this->trans('commands.generate.plugin.validationconstraint.questions.class'), | ||
'ExampleConstraint', | ||
function ($className) { | ||
return $this->validator->validateClassName($className); | ||
} | ||
); | ||
$input->setOption('class', $className); | ||
} | ||
|
||
// --plugin label option | ||
$label = $input->getOption('label'); | ||
if (!$label) { | ||
$label = $this->getIo()->ask( | ||
$this->trans('commands.generate.plugin.validationconstraint.questions.label'), | ||
$this->stringConverter->camelCaseToHuman($className) | ||
); | ||
$input->setOption('label', $label); | ||
} | ||
|
||
// --plugin-id option | ||
$pluginId = $input->getOption('plugin-id'); | ||
if (!$pluginId) { | ||
$pluginId = $this->getIo()->ask( | ||
$this->trans('commands.generate.plugin.validationconstraint.questions.plugin-id'), | ||
$this->stringConverter->camelCaseToUnderscore($className) | ||
); | ||
$input->setOption('plugin-id', $pluginId); | ||
} | ||
|
||
$hook = $this->getIo()->confirm( | ||
$this->trans('commands.generate.plugin.validationconstraint.questions.hook'), | ||
false | ||
); | ||
if (!empty($hook)) { | ||
$fieldId = $this->getIo()->ask( | ||
$this->trans('commands.generate.plugin.validationconstraint.questions.field-id') | ||
); | ||
$input->setOption('field-id', $fieldId); | ||
|
||
$bundle = $this->getIo()->ask( | ||
$this->trans('commands.generate.plugin.validationconstraint.questions.bundle') | ||
); | ||
$input->setOption('bundle', $bundle); | ||
} | ||
$input->setOption('hook', $hook); | ||
} | ||
} |
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,63 @@ | ||
<?php | ||
|
||
/** | ||
* @file | ||
* Contains \Drupal\Console\Generator\PluginValidationConstraintGenerator. | ||
*/ | ||
|
||
namespace Drupal\Console\Generator; | ||
|
||
use Drupal\Console\Core\Generator\Generator; | ||
use Drupal\Console\Extension\Manager; | ||
|
||
class PluginValidationConstraintGenerator extends Generator | ||
{ | ||
|
||
/** | ||
* @var Manager | ||
*/ | ||
protected $extensionManager; | ||
|
||
/** | ||
* PluginValidationConstraintGenerator constructor. | ||
* | ||
* @param Manager $extensionManager | ||
*/ | ||
public function __construct(Manager $extensionManager) { | ||
$this->extensionManager = $extensionManager; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function generate(array $parameters) | ||
{ | ||
$module = $parameters['module']; | ||
$className = $parameters['class_name']; | ||
$hook = $parameters['hook']; | ||
$pluginPath = $this->extensionManager->getPluginPath($module, 'Validation/Constraint') . '/' . $className; | ||
|
||
// Generates Contraint class. | ||
$this->renderFile( | ||
'module/src/Plugin/Validation/Constraint/constraint.php.twig', | ||
$pluginPath . '.php', | ||
$parameters | ||
); | ||
|
||
// Generates Validator class. | ||
$this->renderFile( | ||
'module/src/Plugin/Validation/Constraint/validator.php.twig', | ||
$pluginPath . 'Validator.php', | ||
$parameters | ||
); | ||
|
||
if (!empty($hook)) { | ||
$this->renderFile( | ||
'module/src/Entity/Bundle/entity-bundle-field-info-alter.php.twig', | ||
$this->extensionManager->getModule($module)->getPath() . '/' . $module . '.module', | ||
$parameters, | ||
FILE_APPEND | ||
); | ||
} | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
templates/module/src/Entity/Bundle/entity-bundle-field-info-alter.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,20 @@ | ||
|
||
{% block use_class %} | ||
use Drupal\Core\Entity\EntityTypeInterface; | ||
{% endblock %} | ||
|
||
/** | ||
* Implements hook_entity_bundle_field_info_alter(). | ||
*/ | ||
function {{module}}_entity_bundle_field_info_alter(&$fields, EntityTypeInterface $entity_type, $bundle) { | ||
{% if bundle %} | ||
if ($bundle === '{{ bundle }}') { | ||
{% endif %} | ||
if (isset($fields['{{ field_id }}'])) { | ||
// Use the ID as defined in the annotation of the constraint definition. | ||
$fields['{{ field_id }}']->addConstraint('{{ plugin_id }}', []); | ||
} | ||
{% if bundle %} | ||
} | ||
{% endif %} | ||
} |
34 changes: 34 additions & 0 deletions
34
templates/module/src/Plugin/Validation/Constraint/constraint.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,34 @@ | ||
{% extends "base/class.php.twig" %} | ||
|
||
{% block file_path %} | ||
\Drupal\{{ module }}\Plugin\Validation\Constraint\{{ class_name }}. | ||
{% endblock %} | ||
|
||
{% block namespace_class %} | ||
namespace Drupal\{{ module }}\Plugin\Validation\Constraint; | ||
{% endblock %} | ||
|
||
{% block use_class %} | ||
use Symfony\Component\Validator\Constraint; | ||
use Symfony\Component\Validator\ConstraintValidator; | ||
{% endblock %} | ||
|
||
{% block class_declaration %} | ||
/** | ||
* Plugin implementation of the '{{ plugin_id }}'. | ||
* | ||
* @Constraint( | ||
* id = "{{ plugin_id }}", | ||
* label = @Translation("{{ label }}", context = "Validation"), | ||
* ) | ||
*/ | ||
class {{ class_name }} extends Constraint | ||
{% endblock %} | ||
|
||
{% block class_properties %} | ||
// The message that will be shown if the value is empty. | ||
public $isEmpty = '%value is empty'; | ||
|
||
// The message that will be shown if the value is not unique. | ||
public $notUnique = '%value is not unique'; | ||
{% endblock %} |
Oops, something went wrong.