Version 1
[yaffs-website] / web / core / modules / forum / src / Plugin / Validation / Constraint / ForumLeafConstraintValidator.php
diff --git a/web/core/modules/forum/src/Plugin/Validation/Constraint/ForumLeafConstraintValidator.php b/web/core/modules/forum/src/Plugin/Validation/Constraint/ForumLeafConstraintValidator.php
new file mode 100644 (file)
index 0000000..95603d8
--- /dev/null
@@ -0,0 +1,33 @@
+<?php
+
+namespace Drupal\forum\Plugin\Validation\Constraint;
+
+use Symfony\Component\Validator\Constraint;
+use Symfony\Component\Validator\ConstraintValidator;
+
+/**
+ * Validates the ForumLeaf constraint.
+ */
+class ForumLeafConstraintValidator extends ConstraintValidator {
+
+  /**
+   * {@inheritdoc}
+   */
+  public function validate($items, Constraint $constraint) {
+    $item = $items->first();
+    if (!isset($item)) {
+      return NULL;
+    }
+
+    // Verify that a term has been selected.
+    if (!$item->entity) {
+      $this->context->addViolation($constraint->selectForum);
+    }
+
+    // The forum_container flag must not be set.
+    if (!empty($item->entity->forum_container->value)) {
+      $this->context->addViolation($constraint->noLeafMessage, ['%forum' => $item->entity->getName()]);
+    }
+  }
+
+}