diff 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
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/vendor/chi-teck/drupal-code-generator/templates/d8/plugin/constraint-validator.twig	Thu Feb 28 13:11:55 2019 +0000
@@ -0,0 +1,62 @@
+<?php
+
+namespace Drupal\{{ machine_name }}\Plugin\Validation\Constraint;
+
+use Symfony\Component\Validator\Constraint;
+use Symfony\Component\Validator\ConstraintValidator;
+
+/**
+ * Validates the {{ plugin_label }} constraint.
+ */
+class {{ class }}Validator extends ConstraintValidator {
+
+  /**
+   * {@inheritdoc}
+   */
+{% if input_type == 'entity' %}
+  public function validate($entity, Constraint $constraint) {
+
+    // @DCG Validate the entity here.
+    if ($entity->label() == 'foo') {
+      $this->context->buildViolation($constraint->errorMessage)
+        // @DCG The path depends on entity type. It can be title, name, etc.
+        ->atPath('title')
+        ->addViolation();
+    }
+
+  }
+{% elseif input_type == 'item_list' %}
+  public function validate($items, Constraint $constraint) {
+
+    foreach ($items as $delta => $item) {
+      // @DCG Validate the item here.
+      if ($item->value == 'foo') {
+        $this->context->buildViolation($constraint->errorMessage)
+          ->atPath($delta)
+          ->addViolation();
+      }
+    }
+
+  }
+{% elseif input_type == 'item' %}
+  public function validate($item, Constraint $constraint) {
+
+    $value = $item->getValue()['value'];
+    // @DCG Validate the value here.
+    if ($value == 'foo') {
+      $this->context->addViolation($constraint->errorMessage);
+    }
+
+  }
+{% else %}
+  public function validate($value, Constraint $constraint) {
+
+    // @DCG Validate the value here.
+    if ($value == 'foo') {
+      $this->context->addViolation($constraint->errorMessage);
+    }
+
+  }
+{% endif %}
+
+}