diff --git a/core/core.services.yml b/core/core.services.yml index 03d9ffe..cd53490 100644 --- a/core/core.services.yml +++ b/core/core.services.yml @@ -484,6 +484,7 @@ services: typed_data_manager: class: Drupal\Core\TypedData\TypedDataManager parent: default_plugin_manager + arguments: ['@container.namespaces', '@cache.discovery', '@module_handler', '@class_resolver'] calls: - [setValidationConstraintManager, ['@validation.constraint']] tags: diff --git a/core/lib/Drupal/Core/TypedData/TypedDataManager.php b/core/lib/Drupal/Core/TypedData/TypedDataManager.php index f760ddc..955be72 100644 --- a/core/lib/Drupal/Core/TypedData/TypedDataManager.php +++ b/core/lib/Drupal/Core/TypedData/TypedDataManager.php @@ -10,6 +10,7 @@ use Drupal\Component\Plugin\Exception\PluginException; use Drupal\Component\Utility\String; use Drupal\Core\Cache\CacheBackendInterface; +use Drupal\Core\DependencyInjection\ClassResolverInterface; use Drupal\Core\Extension\ModuleHandlerInterface; use Drupal\Core\Plugin\DefaultPluginManager; use Drupal\Core\TypedData\Validation\MetadataFactory; @@ -45,20 +46,30 @@ class TypedDataManager extends DefaultPluginManager { */ protected $prototypes = array(); - /** - * Constructs a new TypedDataManager. - * - * @param \Traversable $namespaces - * An object that implements \Traversable which contains the root paths - * keyed by the corresponding namespace to look for plugin implementations. - * @param \Drupal\Core\Cache\CacheBackendInterface $cache_backend - * Cache backend instance to use. - * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler - * The module handler. - */ - public function __construct(\Traversable $namespaces, CacheBackendInterface $cache_backend, ModuleHandlerInterface $module_handler) { + /** + * The class resolver. + * + * @var \Drupal\Core\DependencyInjection\ClassResolverInterface + */ + protected $classResolver; + + /** + * Constructs a new TypedDataManager. + * + * @param \Traversable $namespaces + * An object that implements \Traversable which contains the root paths + * keyed by the corresponding namespace to look for plugin implementations. + * @param \Drupal\Core\Cache\CacheBackendInterface $cache_backend + * Cache backend instance to use. + * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler + * The module handler. + * @param \Drupal\Core\DependencyInjection\ClassResolverInterface $class_resolver + * The class resolver. + */ + public function __construct(\Traversable $namespaces, CacheBackendInterface $cache_backend, ModuleHandlerInterface $module_handler, ClassResolverInterface $class_resolver) { $this->alterInfo('data_type_info'); $this->setCacheBackend($cache_backend, 'typed_data_types_plugins'); + $this->classResolver = $class_resolver; parent::__construct('Plugin/DataType', $namespaces, $module_handler, NULL, 'Drupal\Core\TypedData\Annotation\DataType'); } @@ -323,7 +334,7 @@ public function getValidator() { $this->validator = Validation::createValidatorBuilder() ->setMetadataFactory(new MetadataFactory()) ->setTranslator(new DrupalTranslator()) - ->setConstraintValidatorFactory(new ConstraintValidatorFactory()) + ->setConstraintValidatorFactory(new ConstraintValidatorFactory($this->classResolver)) ->setApiVersion(Validation::API_VERSION_2_4) ->getValidator(); } diff --git a/core/lib/Drupal/Core/Validation/ConstraintValidatorFactory.php b/core/lib/Drupal/Core/Validation/ConstraintValidatorFactory.php index f556348..b9d9f94 100644 --- a/core/lib/Drupal/Core/Validation/ConstraintValidatorFactory.php +++ b/core/lib/Drupal/Core/Validation/ConstraintValidatorFactory.php @@ -7,8 +7,8 @@ namespace Drupal\Core\Validation; +use Drupal\Core\DependencyInjection\ClassResolverInterface; use Symfony\Component\Validator\Constraint; -use Symfony\Component\Validator\Constraints\ExpressionValidator; use Symfony\Component\Validator\ConstraintValidatorFactory as BaseConstraintValidatorFactory; /** @@ -17,9 +17,13 @@ class ConstraintValidatorFactory extends BaseConstraintValidatorFactory { /** - * {@inheritdoc} + * Constructs a new ConstraintValidatorFactory. + * + * @param \Drupal\Core\DependencyInjection\ClassResolverInterface $class_resolver */ - protected $propertyAccessor; + public function __construct(ClassResolverInterface $class_resolver) { + $this->classResolver = $class_resolver; + } /** * {@inheritdoc} @@ -28,15 +32,10 @@ public function getInstance(Constraint $constraint) { $class_name = $constraint->validatedBy(); if (!isset($this->validators[$class_name])) { - // If the plugin provides a factory method, pass the container to it. - if (is_subclass_of($class_name, 'Drupal\Core\Plugin\ContainerFactoryPluginInterface')) { - $this->validators[$class_name] = $class_name::create(\Drupal::getContainer(), [], NULL, []); - } - else { - $this->validators[$class_name] = 'validator.expression' === $class_name ? new ExpressionValidator($this->propertyAccessor) : new $class_name(); - } + $this->validators[$class_name] = $this->classResolver->getInstanceFromDefinition($class_name); } return $this->validators[$class_name]; } + }