From a17e648a243c5cd8766a5a587ee3b5e483a54ce3 Mon Sep 17 00:00:00 2001
From: catch <6915-catch@users.noreply.drupalcode.org>
Date: Mon, 8 Jul 2024 15:49:30 +0100
Subject: [PATCH] Issue #3449851 by Liam Morland, alexpott, smustgrave,
 solideogloria, catch, kopeboy: Replace LogicException with trigger_error in
 LangcodeRequiredIfTranslatableValues constraint

---
 ...dIfTranslatableValuesConstraintValidator.php | 10 +++++++---
 .../config/schema/config_test.schema.yml        |  7 +++++++
 .../Core/Config/ConfigSchemaTest.php            | 17 +++++++++++++++++
 3 files changed, 31 insertions(+), 3 deletions(-)

diff --git a/core/lib/Drupal/Core/Config/Plugin/Validation/Constraint/LangcodeRequiredIfTranslatableValuesConstraintValidator.php b/core/lib/Drupal/Core/Config/Plugin/Validation/Constraint/LangcodeRequiredIfTranslatableValuesConstraintValidator.php
index 89a45e7b18dc..160fbd1cc124 100644
--- a/core/lib/Drupal/Core/Config/Plugin/Validation/Constraint/LangcodeRequiredIfTranslatableValuesConstraintValidator.php
+++ b/core/lib/Drupal/Core/Config/Plugin/Validation/Constraint/LangcodeRequiredIfTranslatableValuesConstraintValidator.php
@@ -7,7 +7,6 @@
 use Drupal\Core\Config\Schema\Mapping;
 use Symfony\Component\Validator\Constraint;
 use Symfony\Component\Validator\ConstraintValidator;
-use Symfony\Component\Validator\Exception\LogicException;
 
 /**
  * Validates the LangcodeRequiredIfTranslatableValues constraint.
@@ -22,8 +21,13 @@ public function validate(mixed $value, Constraint $constraint): void {
 
     $mapping = $this->context->getObject();
     assert($mapping instanceof Mapping);
-    if ($mapping !== $this->context->getRoot()) {
-      throw new LogicException('The LangcodeRequiredIfTranslatableValues constraint can only operate on the root object being validated.');
+    $root = $this->context->getRoot();
+    if ($mapping !== $root) {
+      @trigger_error(sprintf(
+        'The LangcodeRequiredIfTranslatableValues constraint can only be applied to the root object being validated, using the \'config_object\' schema type on \'%s\' is deprecated in drupal:10.3.0 and will trigger a \LogicException in drupal:11.0.0. See https://www.drupal.org/node/3459863',
+        $root->getName() . '::' . $mapping->getName()
+      ), E_USER_DEPRECATED);
+      return;
     }
 
     assert(in_array('langcode', $mapping->getValidKeys(), TRUE));
diff --git a/core/modules/config/tests/config_test/config/schema/config_test.schema.yml b/core/modules/config/tests/config_test/config/schema/config_test.schema.yml
index 99b7cfd8f064..aeac1edea756 100644
--- a/core/modules/config/tests/config_test/config/schema/config_test.schema.yml
+++ b/core/modules/config/tests/config_test/config/schema/config_test.schema.yml
@@ -241,6 +241,13 @@ config_test.foo:
     label:
       type: label
       label: 'Label'
+    # Note that config_object should never be used on a non-root key.
+    broken_langcode_required:
+      type: config_object
+      required: false
+      mapping:
+        foo:
+          type: string
 
 config_test.bar:
   type: config_test.foo
diff --git a/core/tests/Drupal/KernelTests/Core/Config/ConfigSchemaTest.php b/core/tests/Drupal/KernelTests/Core/Config/ConfigSchemaTest.php
index 6c0fcf5d42e5..e079a4ee90bd 100644
--- a/core/tests/Drupal/KernelTests/Core/Config/ConfigSchemaTest.php
+++ b/core/tests/Drupal/KernelTests/Core/Config/ConfigSchemaTest.php
@@ -854,4 +854,21 @@ public function testConfigSaveWithWrappingSchemaDoubleBrackets(): void {
     ], $definition['mapping']['breed']);
   }
 
+  /**
+   * @group legacy
+   */
+  public function testLangcodeRequiredIfTranslatableValuesConstraintError(): void {
+    $config = \Drupal::configFactory()->getEditable('config_test.foo');
+
+    $config
+      ->set('broken_langcode_required.foo', 'bar')
+      ->save();
+
+    $this->expectDeprecation('The LangcodeRequiredIfTranslatableValues constraint can only be applied to the root object being validated, using the \'config_object\' schema type on \'config_test.foo::broken_langcode_required\' is deprecated in drupal:10.3.0 and will trigger a \LogicException in drupal:11.0.0. See https://www.drupal.org/node/3459863');
+    $violations = \Drupal::service('config.typed')->createFromNameAndData($config->getName(), $config->get())
+      ->validate();
+
+    $this->assertCount(0, $violations);
+  }
+
 }
-- 
GitLab