From 97c49565b1085b33b6559a3a9da88140c2051b21 Mon Sep 17 00:00:00 2001
From: catch <catch@35733.no-reply.drupal.org>
Date: Fri, 9 Feb 2024 09:43:35 +0000
Subject: [PATCH] Issue #3416549 by Wim Leers: ConfigExistsConstraintValidator
 should ignore NULL values

---
 .../Constraint/ConfigExistsConstraintValidator.php           | 5 +++++
 .../Core/Config/ConfigExistsConstraintValidatorTest.php      | 4 ++++
 2 files changed, 9 insertions(+)

diff --git a/core/lib/Drupal/Core/Config/Plugin/Validation/Constraint/ConfigExistsConstraintValidator.php b/core/lib/Drupal/Core/Config/Plugin/Validation/Constraint/ConfigExistsConstraintValidator.php
index 590a3056a110..83067c2c1b59 100644
--- a/core/lib/Drupal/Core/Config/Plugin/Validation/Constraint/ConfigExistsConstraintValidator.php
+++ b/core/lib/Drupal/Core/Config/Plugin/Validation/Constraint/ConfigExistsConstraintValidator.php
@@ -43,6 +43,11 @@ public static function create(ContainerInterface $container) {
    * {@inheritdoc}
    */
   public function validate(mixed $name, Constraint $constraint) {
+    // This constraint may be used to validate nullable (optional) values.
+    if ($name === NULL) {
+      return;
+    }
+
     if (!in_array($name, $this->configFactory->listAll(), TRUE)) {
       $this->context->addViolation($constraint->message, ['@name' => $name]);
     }
diff --git a/core/tests/Drupal/KernelTests/Core/Config/ConfigExistsConstraintValidatorTest.php b/core/tests/Drupal/KernelTests/Core/Config/ConfigExistsConstraintValidatorTest.php
index 03992fd9c921..bb94ae83d795 100644
--- a/core/tests/Drupal/KernelTests/Core/Config/ConfigExistsConstraintValidatorTest.php
+++ b/core/tests/Drupal/KernelTests/Core/Config/ConfigExistsConstraintValidatorTest.php
@@ -40,6 +40,10 @@ public function testValidation(): void {
 
     $this->installConfig('system');
     $this->assertCount(0, $data->validate());
+
+    // NULL should not trigger a validation error: a value may be nullable.
+    $data->setValue(NULL);
+    $this->assertCount(0, $data->validate());
   }
 
 }
-- 
GitLab