From 9aa56e0ab2e5880283ba1075be2140a13b909bed Mon Sep 17 00:00:00 2001 From: chesn0k Date: Tue, 21 May 2024 18:56:04 +0300 Subject: [PATCH] Use PHP attribute for constraint plugin. --- templates/Plugin/_constraint/constraint.twig | 15 ++++++++------- .../Validation/Constraint/DeltaConstraint.php | 15 ++++++++------- .../Validation/Constraint/BetaConstraint.php | 15 ++++++++------- .../Validation/Constraint/GammaConstraint.php | 15 ++++++++------- .../Validation/Constraint/AlphaConstraint.php | 15 ++++++++------- .../Validation/Constraint/AlphaConstraint.php | 15 ++++++++------- 6 files changed, 48 insertions(+), 42 deletions(-) diff --git a/templates/Plugin/_constraint/constraint.twig b/templates/Plugin/_constraint/constraint.twig index de5400701..3eecb5f4d 100644 --- a/templates/Plugin/_constraint/constraint.twig +++ b/templates/Plugin/_constraint/constraint.twig @@ -4,15 +4,12 @@ declare(strict_types=1); namespace Drupal\{{ machine_name }}\Plugin\Validation\Constraint; -use Symfony\Component\Validator\Constraint; +use Drupal\Core\StringTranslation\TranslatableMarkup; +use Drupal\Core\Validation\Attribute\Constraint; +use Symfony\Component\Validator\Constraint as SymfonyConstraint; /** * Provides {{ plugin_label|article }} constraint. - * - * @Constraint( - * id = "{{ plugin_id }}", - * label = @Translation("{{ plugin_label }}", context = "Validation"), - * ) {% if input_type == 'entity' %} * * @see https://www.drupal.org/node/2015723. @@ -37,7 +34,11 @@ use Symfony\Component\Validator\Constraint; * @see https://www.drupal.org/node/2015723 {% endif %} */ -final class {{ class }} extends Constraint { +#[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.'; diff --git a/tests/functional/Generator/Plugin/_constraint/_n_deps/_entity/src/Plugin/Validation/Constraint/DeltaConstraint.php b/tests/functional/Generator/Plugin/_constraint/_n_deps/_entity/src/Plugin/Validation/Constraint/DeltaConstraint.php index 5135a71dd..dcea06f0f 100644 --- a/tests/functional/Generator/Plugin/_constraint/_n_deps/_entity/src/Plugin/Validation/Constraint/DeltaConstraint.php +++ b/tests/functional/Generator/Plugin/_constraint/_n_deps/_entity/src/Plugin/Validation/Constraint/DeltaConstraint.php @@ -4,19 +4,20 @@ namespace Drupal\foo\Plugin\Validation\Constraint; -use Symfony\Component\Validator\Constraint; +use Drupal\Core\StringTranslation\TranslatableMarkup; +use Drupal\Core\Validation\Attribute\Constraint; +use Symfony\Component\Validator\Constraint as SymfonyConstraint; /** * Provides a Delta constraint. * - * @Constraint( - * id = "FooDelta", - * label = @Translation("Delta", context = "Validation"), - * ) - * * @see https://www.drupal.org/node/2015723. */ -final class DeltaConstraint extends Constraint { +#[Constraint( + id: 'FooDelta', + label: new TranslatableMarkup('Delta', options: ['context' => 'Validation']) +)] +final class DeltaConstraint extends SymfonyConstraint { public string $message = '@todo Specify error message here.'; diff --git a/tests/functional/Generator/Plugin/_constraint/_n_deps/_item/src/Plugin/Validation/Constraint/BetaConstraint.php b/tests/functional/Generator/Plugin/_constraint/_n_deps/_item/src/Plugin/Validation/Constraint/BetaConstraint.php index 1d06bc0f3..d88537f32 100644 --- a/tests/functional/Generator/Plugin/_constraint/_n_deps/_item/src/Plugin/Validation/Constraint/BetaConstraint.php +++ b/tests/functional/Generator/Plugin/_constraint/_n_deps/_item/src/Plugin/Validation/Constraint/BetaConstraint.php @@ -4,16 +4,13 @@ namespace Drupal\foo\Plugin\Validation\Constraint; -use Symfony\Component\Validator\Constraint; +use Drupal\Core\StringTranslation\TranslatableMarkup; +use Drupal\Core\Validation\Attribute\Constraint; +use Symfony\Component\Validator\Constraint as SymfonyConstraint; /** * Provides a Beta constraint. * - * @Constraint( - * id = "FooBeta", - * label = @Translation("Beta", context = "Validation"), - * ) - * * @DCG * To apply this constraint on third party field types. Implement * hook_field_info_alter() as follows. @@ -25,7 +22,11 @@ * * @see https://www.drupal.org/node/2015723 */ -final class BetaConstraint extends Constraint { +#[Constraint( + id: 'FooBeta', + label: new TranslatableMarkup('Beta', options: ['context' => 'Validation']) +)] +final class BetaConstraint extends SymfonyConstraint { public string $message = '@todo Specify error message here.'; diff --git a/tests/functional/Generator/Plugin/_constraint/_n_deps/_item_list/src/Plugin/Validation/Constraint/GammaConstraint.php b/tests/functional/Generator/Plugin/_constraint/_n_deps/_item_list/src/Plugin/Validation/Constraint/GammaConstraint.php index 817917482..7bc8908f3 100644 --- a/tests/functional/Generator/Plugin/_constraint/_n_deps/_item_list/src/Plugin/Validation/Constraint/GammaConstraint.php +++ b/tests/functional/Generator/Plugin/_constraint/_n_deps/_item_list/src/Plugin/Validation/Constraint/GammaConstraint.php @@ -4,23 +4,24 @@ namespace Drupal\foo\Plugin\Validation\Constraint; -use Symfony\Component\Validator\Constraint; +use Drupal\Core\StringTranslation\TranslatableMarkup; +use Drupal\Core\Validation\Attribute\Constraint; +use Symfony\Component\Validator\Constraint as SymfonyConstraint; /** * Provides a Gamma constraint. * - * @Constraint( - * id = "FooGamma", - * label = @Translation("Gamma", context = "Validation"), - * ) - * * @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 */ -final class GammaConstraint extends Constraint { +#[Constraint( + id: 'FooGamma', + label: new TranslatableMarkup('Gamma', options: ['context' => 'Validation']) +)] +final class GammaConstraint extends SymfonyConstraint { public string $message = '@todo Specify error message here.'; diff --git a/tests/functional/Generator/Plugin/_constraint/_n_deps/_raw_value/src/Plugin/Validation/Constraint/AlphaConstraint.php b/tests/functional/Generator/Plugin/_constraint/_n_deps/_raw_value/src/Plugin/Validation/Constraint/AlphaConstraint.php index 86deb1c2a..856d88733 100644 --- a/tests/functional/Generator/Plugin/_constraint/_n_deps/_raw_value/src/Plugin/Validation/Constraint/AlphaConstraint.php +++ b/tests/functional/Generator/Plugin/_constraint/_n_deps/_raw_value/src/Plugin/Validation/Constraint/AlphaConstraint.php @@ -4,17 +4,18 @@ namespace Drupal\foo\Plugin\Validation\Constraint; -use Symfony\Component\Validator\Constraint; +use Drupal\Core\StringTranslation\TranslatableMarkup; +use Drupal\Core\Validation\Attribute\Constraint; +use Symfony\Component\Validator\Constraint as SymfonyConstraint; /** * Provides an Alpha constraint. - * - * @Constraint( - * id = "FooAlpha", - * label = @Translation("Alpha", context = "Validation"), - * ) */ -final class AlphaConstraint extends Constraint { +#[Constraint( + id: 'FooAlpha', + label: new TranslatableMarkup('Alpha', options: ['context' => 'Validation']) +)] +final class AlphaConstraint extends SymfonyConstraint { public string $message = '@todo Specify error message here.'; diff --git a/tests/functional/Generator/Plugin/_constraint/_w_deps/_raw_value/src/Plugin/Validation/Constraint/AlphaConstraint.php b/tests/functional/Generator/Plugin/_constraint/_w_deps/_raw_value/src/Plugin/Validation/Constraint/AlphaConstraint.php index 86deb1c2a..856d88733 100644 --- a/tests/functional/Generator/Plugin/_constraint/_w_deps/_raw_value/src/Plugin/Validation/Constraint/AlphaConstraint.php +++ b/tests/functional/Generator/Plugin/_constraint/_w_deps/_raw_value/src/Plugin/Validation/Constraint/AlphaConstraint.php @@ -4,17 +4,18 @@ namespace Drupal\foo\Plugin\Validation\Constraint; -use Symfony\Component\Validator\Constraint; +use Drupal\Core\StringTranslation\TranslatableMarkup; +use Drupal\Core\Validation\Attribute\Constraint; +use Symfony\Component\Validator\Constraint as SymfonyConstraint; /** * Provides an Alpha constraint. - * - * @Constraint( - * id = "FooAlpha", - * label = @Translation("Alpha", context = "Validation"), - * ) */ -final class AlphaConstraint extends Constraint { +#[Constraint( + id: 'FooAlpha', + label: new TranslatableMarkup('Alpha', options: ['context' => 'Validation']) +)] +final class AlphaConstraint extends SymfonyConstraint { public string $message = '@todo Specify error message here.';