Initial Drupal 11 with DDEV setup
This commit is contained in:
45
vendor/chi-teck/drupal-code-generator/templates/Plugin/_constraint/constraint.twig
vendored
Normal file
45
vendor/chi-teck/drupal-code-generator/templates/Plugin/_constraint/constraint.twig
vendored
Normal file
@ -0,0 +1,45 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Drupal\{{ machine_name }}\Plugin\Validation\Constraint;
|
||||
|
||||
use Drupal\Core\StringTranslation\TranslatableMarkup;
|
||||
use Drupal\Core\Validation\Attribute\Constraint;
|
||||
use Symfony\Component\Validator\Constraint as SymfonyConstraint;
|
||||
|
||||
/**
|
||||
* Provides {{ plugin_label|article }} constraint.
|
||||
{% if input_type == 'entity' %}
|
||||
*
|
||||
* @see https://www.drupal.org/node/2015723.
|
||||
{% elseif input_type == 'item_list' %}
|
||||
*
|
||||
* @DCG
|
||||
* To apply this constraint on third party entity types implement either
|
||||
* hook_entity_base_field_info_alter() or hook_entity_bundle_field_info_alter().
|
||||
*
|
||||
* @see https://www.drupal.org/node/2015723
|
||||
{% elseif input_type == 'item' %}
|
||||
*
|
||||
* @DCG
|
||||
* To apply this constraint on third party field types. Implement
|
||||
* hook_field_info_alter() as follows.
|
||||
* @code
|
||||
* function {{ machine_name }}_field_info_alter(array &$info): void {
|
||||
* $info['FIELD_TYPE']['constraints']['{{ plugin_id }}'] = [];
|
||||
* }
|
||||
* @endcode
|
||||
*
|
||||
* @see https://www.drupal.org/node/2015723
|
||||
{% endif %}
|
||||
*/
|
||||
#[Constraint(
|
||||
id: '{{ plugin_id }}',
|
||||
label: new TranslatableMarkup('{{ plugin_label }}', options: ['context' => 'Validation'])
|
||||
)]
|
||||
final class {{ class }} extends SymfonyConstraint {
|
||||
|
||||
public string $message = '@todo Specify error message here.';
|
||||
|
||||
}
|
||||
106
vendor/chi-teck/drupal-code-generator/templates/Plugin/_constraint/validator.twig
vendored
Normal file
106
vendor/chi-teck/drupal-code-generator/templates/Plugin/_constraint/validator.twig
vendored
Normal file
@ -0,0 +1,106 @@
|
||||
{% import '@lib/di.twig' as di %}
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Drupal\{{ machine_name }}\Plugin\Validation\Constraint;
|
||||
|
||||
{% apply sort_namespaces %}
|
||||
use Symfony\Component\Validator\Constraint;
|
||||
use Symfony\Component\Validator\ConstraintValidator;
|
||||
{% if input_type == 'item' %}
|
||||
use Drupal\Core\Field\FieldItemInterface;
|
||||
{% elseif input_type == 'item_list' %}
|
||||
use Drupal\Core\Field\FieldItemListInterface;
|
||||
{% elseif input_type == 'entity' %}
|
||||
use Drupal\Core\Entity\EntityInterface;
|
||||
{% endif %}
|
||||
{% if services %}
|
||||
{{ di.use(services) }}
|
||||
use Drupal\Core\DependencyInjection\ContainerInjectionInterface;
|
||||
use Symfony\Component\DependencyInjection\ContainerInterface;
|
||||
{% endif %}
|
||||
{% endapply %}
|
||||
|
||||
/**
|
||||
* Validates the {{ plugin_label }} constraint.
|
||||
*/
|
||||
final class {{ class }}Validator extends ConstraintValidator {% if services %}implements ContainerInjectionInterface {% endif %}{
|
||||
|
||||
{% if services %}
|
||||
/**
|
||||
* Constructs the object.
|
||||
*/
|
||||
public function __construct(
|
||||
{{ di.signature(services) }}
|
||||
) {}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public static function create(ContainerInterface $container): self {
|
||||
return new self(
|
||||
{{ di.container(services) }}
|
||||
);
|
||||
}
|
||||
|
||||
{% endif %}
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
{% if input_type == 'raw_value' %}
|
||||
public function validate(mixed $value, Constraint $constraint): void {
|
||||
// @todo Validate the value here.
|
||||
if ($value === 'wrong') {
|
||||
$this->context->addViolation($constraint->message);
|
||||
}
|
||||
}
|
||||
{% elseif input_type == 'item' %}
|
||||
public function validate(mixed $item, Constraint $constraint): void {
|
||||
if (!$item instanceof FieldItemInterface) {
|
||||
throw new \InvalidArgumentException(
|
||||
sprintf('The validated value must be instance of \Drupal\Core\Field\FieldItemInterface, %s was given.', get_debug_type($item))
|
||||
);
|
||||
}
|
||||
// @todo Validate the item value here.
|
||||
if ($item->value === 'wrong') {
|
||||
$this->context->addViolation($constraint->message);
|
||||
}
|
||||
}
|
||||
{% elseif input_type == 'item_list' %}
|
||||
public function validate(mixed $items, Constraint $constraint): void {
|
||||
if (!$items instanceof FieldItemListInterface) {
|
||||
throw new \InvalidArgumentException(
|
||||
sprintf('The validated value must be instance of \Drupal\Core\Field\FieldItemListInterface, %s was given.', get_debug_type($items))
|
||||
);
|
||||
}
|
||||
foreach ($items as $delta => $item) {
|
||||
// @todo Validate the item list here.
|
||||
if ($item->value === 'wrong') {
|
||||
$this->context->buildViolation($constraint->message)
|
||||
->atPath($delta)
|
||||
->addViolation();
|
||||
}
|
||||
}
|
||||
}
|
||||
{% elseif input_type == 'entity' %}
|
||||
public function validate(mixed $entity, Constraint $constraint): void {
|
||||
if (!$entity instanceof EntityInterface) {
|
||||
throw new \InvalidArgumentException(
|
||||
sprintf('The validated value must be instance of \Drupal\Core\Entity\EntityInterface, %s was given.', get_debug_type($entity))
|
||||
);
|
||||
}
|
||||
// @todo Validate the entity here.
|
||||
if ($entity->label() === 'wrong') {
|
||||
// @DCG Use the following code to bind the violation to a specific field.
|
||||
// @code
|
||||
// $this->context->buildViolation($constraint->message)
|
||||
// ->atPath('field_example')
|
||||
// ->addViolation();
|
||||
// @endcode
|
||||
$this->context->addViolation($constraint->message);
|
||||
}
|
||||
}
|
||||
{% endif %}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user