function RequiredConfigDependenciesConstraintValidator::validate

Same name in other branches
  1. 10 core/lib/Drupal/Core/Config/Plugin/Validation/Constraint/RequiredConfigDependenciesConstraintValidator.php \Drupal\Core\Config\Plugin\Validation\Constraint\RequiredConfigDependenciesConstraintValidator::validate()

File

core/lib/Drupal/Core/Config/Plugin/Validation/Constraint/RequiredConfigDependenciesConstraintValidator.php, line 51

Class

RequiredConfigDependenciesConstraintValidator
Validates the RequiredConfigDependencies constraint.

Namespace

Drupal\Core\Config\Plugin\Validation\Constraint

Code

public function validate(mixed $entity, Constraint $constraint) : void {
    assert($constraint instanceof RequiredConfigDependenciesConstraint);
    // Only config entities can have config dependencies.
    if (!$entity instanceof ConfigEntityInterface) {
        throw new UnexpectedTypeException($entity, ConfigEntityInterface::class);
    }
    $config_dependencies = $entity->getDependencies()['config'] ?? [];
    foreach ($constraint->entityTypes as $entity_type_id) {
        $entity_type = $this->entityTypeManager
            ->getDefinition($entity_type_id);
        if (!$entity_type instanceof ConfigEntityTypeInterface) {
            throw new LogicException("'{$entity_type_id}' is not a config entity type.");
        }
        // Ensure the current entity type's config prefix is found in the config
        // dependencies of the entity being validated.
        $pattern = sprintf('/^%s\\.\\w+/', $entity_type->getConfigPrefix());
        if (!preg_grep($pattern, $config_dependencies)) {
            $this->context
                ->addViolation($constraint->message, [
                '@entity_type' => $entity->getEntityType()
                    ->getSingularLabel(),
                '@dependency_type' => $entity_type->getSingularLabel(),
            ]);
        }
    }
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.