Verified Commit e07d50ab authored by Lee Rowlands's avatar Lee Rowlands
Browse files

Issue #3518273 by bbrala, wim leers, phenaproxima: The ConfigExists validation...

Issue #3518273 by bbrala, wim leers, phenaproxima: The ConfigExists validation constraint should support dynamic type expressions
parent 9d55d1e6
Loading
Loading
Loading
Loading
Loading
+3 −0
Original line number Diff line number Diff line
@@ -5,6 +5,7 @@
namespace Drupal\Core\Config\Plugin\Validation\Constraint;

use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Config\Schema\TypeResolver;
use Drupal\Core\DependencyInjection\ContainerInjectionInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\Validator\Constraint;
@@ -50,6 +51,8 @@ public function validate(mixed $name, Constraint $constraint): void {
      return;
    }

    $constraint->prefix = TypeResolver::resolveDynamicTypeName($constraint->prefix, $this->context->getObject());

    if (!in_array($constraint->prefix . $name, $this->configFactory->listAll($constraint->prefix), TRUE)) {
      $this->context->addViolation($constraint->message, ['@name' => $constraint->prefix . $name]);
    }
+6 −1
Original line number Diff line number Diff line
@@ -28,6 +28,7 @@ class ConfigExistsConstraintValidatorTest extends KernelTestBase {
   *
   * @testWith [{}, "system.site", "system.site"]
   *           [{"prefix": "system."}, "site", "system.site"]
   *           [{"prefix": "system.[%parent.reference]."}, "admin", "system.menu.admin"]
   */
  public function testValidation(array $constraint_options, string $value, string $expected_config_name): void {
    // Create a data definition that specifies the value must be a string with
@@ -37,7 +38,11 @@ public function testValidation(array $constraint_options, string $value, string

    /** @var \Drupal\Core\TypedData\TypedDataManagerInterface $typed_data */
    $typed_data = $this->container->get('typed_data_manager');
    $data = $typed_data->create($definition, $value);

    // Create a data definition for the parent data.
    $parent_data_definition = $typed_data->createDataDefinition('map');
    $parent_data = $typed_data->create($parent_data_definition, ['reference' => 'menu']);
    $data = $typed_data->create($definition, $value, 'data_name', $parent_data);

    $violations = $data->validate();
    $this->assertCount(1, $violations);