class RequiredConfigDependenciesConstraintValidator

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

Validates the RequiredConfigDependencies constraint.

Hierarchy

Expanded class hierarchy of RequiredConfigDependenciesConstraintValidator

File

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

Namespace

Drupal\Core\Config\Plugin\Validation\Constraint
View source
class RequiredConfigDependenciesConstraintValidator extends ConstraintValidator implements ContainerInjectionInterface {
    
    /**
     * The entity type manager service.
     *
     * @var \Drupal\Core\Entity\EntityTypeManagerInterface
     */
    protected EntityTypeManagerInterface $entityTypeManager;
    
    /**
     * Constructs a RequiredConfigDependenciesConstraintValidator object.
     *
     * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
     *   The entity type manager service.
     */
    public function __construct(EntityTypeManagerInterface $entity_type_manager) {
        $this->entityTypeManager = $entity_type_manager;
    }
    
    /**
     * {@inheritdoc}
     */
    public static function create(ContainerInterface $container) {
        return new static($container->get('entity_type.manager'));
    }
    
    /**
     * {@inheritdoc}
     */
    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(),
                ]);
            }
        }
    }

}

Members

Title Sort descending Modifiers Object type Summary Overriden Title
RequiredConfigDependenciesConstraintValidator::$entityTypeManager protected property The entity type manager service.
RequiredConfigDependenciesConstraintValidator::create public static function Instantiates a new instance of this class. Overrides ContainerInjectionInterface::create
RequiredConfigDependenciesConstraintValidator::validate public function
RequiredConfigDependenciesConstraintValidator::__construct public function Constructs a RequiredConfigDependenciesConstraintValidator object.

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