Skip to content
Snippets Groups Projects
Commit 7a286515 authored by catch's avatar catch
Browse files

Issue #3422398 by Wim Leers, phenaproxima: Allow specifying a prefix for...

Issue #3422398 by Wim Leers, phenaproxima: Allow specifying a prefix for ConfigExistsConstraint, to enable using it for references to config entities
parent 535d1bc2
No related branches found
No related tags found
No related merge requests found
...@@ -23,4 +23,15 @@ class ConfigExistsConstraint extends Constraint { ...@@ -23,4 +23,15 @@ class ConfigExistsConstraint extends Constraint {
*/ */
public string $message = "The '@name' config does not exist."; public string $message = "The '@name' config does not exist.";
/**
* Optional prefix, to be specified when this contains a config entity ID.
*
* Every config entity type can have multiple instances, all with unique IDs
* but the same config prefix. When config refers to a config entity,
* typically only the ID is stored, not the prefix.
*
* @var string
*/
public string $prefix = '';
} }
...@@ -43,13 +43,15 @@ public static function create(ContainerInterface $container) { ...@@ -43,13 +43,15 @@ public static function create(ContainerInterface $container) {
* {@inheritdoc} * {@inheritdoc}
*/ */
public function validate(mixed $name, Constraint $constraint) { public function validate(mixed $name, Constraint $constraint) {
assert($constraint instanceof ConfigExistsConstraint);
// This constraint may be used to validate nullable (optional) values. // This constraint may be used to validate nullable (optional) values.
if ($name === NULL) { if ($name === NULL) {
return; return;
} }
if (!in_array($name, $this->configFactory->listAll(), TRUE)) { if (!in_array($constraint->prefix . $name, $this->configFactory->listAll($constraint->prefix), TRUE)) {
$this->context->addViolation($constraint->message, ['@name' => $name]); $this->context->addViolation($constraint->message, ['@name' => $constraint->prefix . $name]);
} }
} }
......
...@@ -23,20 +23,23 @@ class ConfigExistsConstraintValidatorTest extends KernelTestBase { ...@@ -23,20 +23,23 @@ class ConfigExistsConstraintValidatorTest extends KernelTestBase {
/** /**
* Tests the ConfigExists constraint validator. * Tests the ConfigExists constraint validator.
*
* @testWith [{}, "system.site", "system.site"]
* [{"prefix": "system."}, "site", "system.site"]
*/ */
public function testValidation(): void { 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 // Create a data definition that specifies the value must be a string with
// the name of an existing piece of config. // the name of an existing piece of config.
$definition = DataDefinition::create('string') $definition = DataDefinition::create('string')
->addConstraint('ConfigExists'); ->addConstraint('ConfigExists', $constraint_options);
/** @var \Drupal\Core\TypedData\TypedDataManagerInterface $typed_data */ /** @var \Drupal\Core\TypedData\TypedDataManagerInterface $typed_data */
$typed_data = $this->container->get('typed_data_manager'); $typed_data = $this->container->get('typed_data_manager');
$data = $typed_data->create($definition, 'system.site'); $data = $typed_data->create($definition, $value);
$violations = $data->validate(); $violations = $data->validate();
$this->assertCount(1, $violations); $this->assertCount(1, $violations);
$this->assertSame("The 'system.site' config does not exist.", (string) $violations->get(0)->getMessage()); $this->assertSame("The '$expected_config_name' config does not exist.", (string) $violations->get(0)->getMessage());
$this->installConfig('system'); $this->installConfig('system');
$this->assertCount(0, $data->validate()); $this->assertCount(0, $data->validate());
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment