Commit f1263c54 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 b12dd10f)
parent 98eea842
Loading
Loading
Loading
Loading
Loading
+6 −2
Original line number Diff line number Diff line
@@ -22,8 +22,12 @@ 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) {
      throw new LogicException(sprintf(
        'The LangcodeRequiredIfTranslatableValues constraint is applied to \'%s\'. This constraint can only operate on the root object being validated.',
        $root->getName() . '::' . $mapping->getName()
      ));
    }

    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
+11 −0
Original line number Diff line number Diff line
@@ -848,4 +848,15 @@ public function testConfigSaveWithWrappingSchemaDoubleBrackets(): void {
    ], $definition['mapping']['breed']);
  }

  public function testLangcodeRequiredIfTranslatableValuesConstraintError(): void {
    $config = \Drupal::configFactory()->getEditable('config_test.foo');

    $this->expectException(\LogicException::class);
    $this->expectExceptionMessage('The LangcodeRequiredIfTranslatableValues constraint is applied to \'config_test.foo::broken_langcode_required\'. This constraint can only operate on the root object being validated.');

    $config
      ->set('broken_langcode_required.foo', 'bar')
      ->save();
  }

}