Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use PHP attribute for constraint plugin. #163

Merged
merged 1 commit into from
May 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 8 additions & 7 deletions templates/Plugin/_constraint/constraint.twig
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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.';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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.';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.';

Expand Down
Loading