Commit f932d803 authored by catch's avatar catch
Browse files

Issue #3449851 by Liam Morland, alexpott, smustgrave, solideogloria, catch,...

Issue #3449851 by Liam Morland, alexpott, smustgrave, solideogloria, catch, kopeboy: Replace LogicException with trigger_error in LangcodeRequiredIfTranslatableValues constraint

(cherry picked from commit a17e648a)
parent 206a3ac2
Loading
Loading
Loading
Loading
Loading
+7 −3
Original line number Diff line number Diff line
@@ -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));
+7 −0
Original line number Diff line number Diff line
@@ -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
+17 −0
Original line number Diff line number Diff line
@@ -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);
  }

}