Pull merge.
[yaffs-website] / vendor / chi-teck / drupal-code-generator / templates / d8 / plugin / constraint-validator.twig
1 <?php
2
3 namespace Drupal\{{ machine_name }}\Plugin\Validation\Constraint;
4
5 use Symfony\Component\Validator\Constraint;
6 use Symfony\Component\Validator\ConstraintValidator;
7
8 /**
9  * Validates the {{ plugin_label }} constraint.
10  */
11 class {{ class }}Validator extends ConstraintValidator {
12
13   /**
14    * {@inheritdoc}
15    */
16 {% if input_type == 'entity' %}
17   public function validate($entity, Constraint $constraint) {
18
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.
23         ->atPath('title')
24         ->addViolation();
25     }
26
27   }
28 {% elseif input_type == 'item_list' %}
29   public function validate($items, Constraint $constraint) {
30
31     foreach ($items as $delta => $item) {
32       // @DCG Validate the item here.
33       if ($item->value == 'foo') {
34         $this->context->buildViolation($constraint->errorMessage)
35           ->atPath($delta)
36           ->addViolation();
37       }
38     }
39
40   }
41 {% elseif input_type == 'item' %}
42   public function validate($item, Constraint $constraint) {
43
44     $value = $item->getValue()['value'];
45     // @DCG Validate the value here.
46     if ($value == 'foo') {
47       $this->context->addViolation($constraint->errorMessage);
48     }
49
50   }
51 {% else %}
52   public function validate($value, Constraint $constraint) {
53
54     // @DCG Validate the value here.
55     if ($value == 'foo') {
56       $this->context->addViolation($constraint->errorMessage);
57     }
58
59   }
60 {% endif %}
61
62 }