3 namespace Drupal\{{ machine_name }}\Plugin\Validation\Constraint;
5 use Symfony\Component\Validator\Constraint;
6 use Symfony\Component\Validator\ConstraintValidator;
9 * Validates the {{ plugin_label }} constraint.
11 class {{ class }}Validator extends ConstraintValidator {
16 {% if input_type == 'entity' %}
17 public function validate($entity, Constraint $constraint) {
19 // @DCG Validate the entity here.
20 if ($entity->label() == 'foo') {
21 $this->context->buildViolation($constraint->errorMessage)
22 // @DCG The path depends on entity type. It can be title, name, etc.
28 {% elseif input_type == 'item_list' %}
29 public function validate($items, Constraint $constraint) {
31 foreach ($items as $delta => $item) {
32 // @DCG Validate the item here.
33 if ($item->value == 'foo') {
34 $this->context->buildViolation($constraint->errorMessage)
41 {% elseif input_type == 'item' %}
42 public function validate($item, Constraint $constraint) {
44 $value = $item->getValue()['value'];
45 // @DCG Validate the value here.
46 if ($value == 'foo') {
47 $this->context->addViolation($constraint->errorMessage);
52 public function validate($value, Constraint $constraint) {
54 // @DCG Validate the value here.
55 if ($value == 'foo') {
56 $this->context->addViolation($constraint->errorMessage);