Mercurial > hg > cmmr2012-drupal-site
annotate vendor/chi-teck/drupal-code-generator/templates/d8/plugin/constraint-validator.twig @ 4:a9cd425dd02b
Update, including to Drupal core 8.6.10
author | Chris Cannam |
---|---|
date | Thu, 28 Feb 2019 13:11:55 +0000 |
parents | |
children |
rev | line source |
---|---|
Chris@4 | 1 <?php |
Chris@4 | 2 |
Chris@4 | 3 namespace Drupal\{{ machine_name }}\Plugin\Validation\Constraint; |
Chris@4 | 4 |
Chris@4 | 5 use Symfony\Component\Validator\Constraint; |
Chris@4 | 6 use Symfony\Component\Validator\ConstraintValidator; |
Chris@4 | 7 |
Chris@4 | 8 /** |
Chris@4 | 9 * Validates the {{ plugin_label }} constraint. |
Chris@4 | 10 */ |
Chris@4 | 11 class {{ class }}Validator extends ConstraintValidator { |
Chris@4 | 12 |
Chris@4 | 13 /** |
Chris@4 | 14 * {@inheritdoc} |
Chris@4 | 15 */ |
Chris@4 | 16 {% if input_type == 'entity' %} |
Chris@4 | 17 public function validate($entity, Constraint $constraint) { |
Chris@4 | 18 |
Chris@4 | 19 // @DCG Validate the entity here. |
Chris@4 | 20 if ($entity->label() == 'foo') { |
Chris@4 | 21 $this->context->buildViolation($constraint->errorMessage) |
Chris@4 | 22 // @DCG The path depends on entity type. It can be title, name, etc. |
Chris@4 | 23 ->atPath('title') |
Chris@4 | 24 ->addViolation(); |
Chris@4 | 25 } |
Chris@4 | 26 |
Chris@4 | 27 } |
Chris@4 | 28 {% elseif input_type == 'item_list' %} |
Chris@4 | 29 public function validate($items, Constraint $constraint) { |
Chris@4 | 30 |
Chris@4 | 31 foreach ($items as $delta => $item) { |
Chris@4 | 32 // @DCG Validate the item here. |
Chris@4 | 33 if ($item->value == 'foo') { |
Chris@4 | 34 $this->context->buildViolation($constraint->errorMessage) |
Chris@4 | 35 ->atPath($delta) |
Chris@4 | 36 ->addViolation(); |
Chris@4 | 37 } |
Chris@4 | 38 } |
Chris@4 | 39 |
Chris@4 | 40 } |
Chris@4 | 41 {% elseif input_type == 'item' %} |
Chris@4 | 42 public function validate($item, Constraint $constraint) { |
Chris@4 | 43 |
Chris@4 | 44 $value = $item->getValue()['value']; |
Chris@4 | 45 // @DCG Validate the value here. |
Chris@4 | 46 if ($value == 'foo') { |
Chris@4 | 47 $this->context->addViolation($constraint->errorMessage); |
Chris@4 | 48 } |
Chris@4 | 49 |
Chris@4 | 50 } |
Chris@4 | 51 {% else %} |
Chris@4 | 52 public function validate($value, Constraint $constraint) { |
Chris@4 | 53 |
Chris@4 | 54 // @DCG Validate the value here. |
Chris@4 | 55 if ($value == 'foo') { |
Chris@4 | 56 $this->context->addViolation($constraint->errorMessage); |
Chris@4 | 57 } |
Chris@4 | 58 |
Chris@4 | 59 } |
Chris@4 | 60 {% endif %} |
Chris@4 | 61 |
Chris@4 | 62 } |