Commit 5f7ec33a authored by alexpott's avatar alexpott

Issue #2197029 by dawehner, larowlan, ParisLiakos: Allow to inject...

Issue #2197029 by dawehner, larowlan, ParisLiakos: Allow to inject dependencies into validation constraints
parent 1e985b0d
......@@ -489,7 +489,7 @@ services:
arguments: [replica]
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:
......
......@@ -10,10 +10,12 @@
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;
use Drupal\Core\Validation\ConstraintManager;
use Drupal\Core\Validation\ConstraintValidatorFactory;
use Drupal\Core\Validation\DrupalTranslator;
use Symfony\Component\Validator\Validation;
use Symfony\Component\Validator\Validator\ValidatorInterface;
......@@ -44,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');
}
......@@ -322,6 +334,7 @@ public function getValidator() {
$this->validator = Validation::createValidatorBuilder()
->setMetadataFactory(new MetadataFactory())
->setTranslator(new DrupalTranslator())
->setConstraintValidatorFactory(new ConstraintValidatorFactory($this->classResolver))
->setApiVersion(Validation::API_VERSION_2_4)
->getValidator();
}
......
<?php
/**
* @file
* Contains \Drupal\Core\Validation\ConstraintValidatorFactory.
*/
namespace Drupal\Core\Validation;
use Drupal\Core\DependencyInjection\ClassResolverInterface;
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\ConstraintValidatorFactory as BaseConstraintValidatorFactory;
/**
* Defines a constraint validator factory that works with container injection.
*/
class ConstraintValidatorFactory extends BaseConstraintValidatorFactory {
/**
* Constructs a new ConstraintValidatorFactory.
*
* @param \Drupal\Core\DependencyInjection\ClassResolverInterface $class_resolver
*/
public function __construct(ClassResolverInterface $class_resolver) {
$this->classResolver = $class_resolver;
}
/**
* {@inheritdoc}
*/
public function getInstance(Constraint $constraint) {
$class_name = $constraint->validatedBy();
if (!isset($this->validators[$class_name])) {
$this->validators[$class_name] = $this->classResolver->getInstanceFromDefinition($class_name);
}
return $this->validators[$class_name];
}
}
......@@ -7,6 +7,9 @@
namespace Drupal\comment\Plugin\Validation\Constraint;
use Drupal\Core\DependencyInjection\ContainerInjectionInterface;
use Drupal\user\UserStorageInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\comment\CommentInterface;
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\ConstraintValidator;
......@@ -14,7 +17,31 @@
/**
* Validates the CommentName constraint.
*/
class CommentNameConstraintValidator extends ConstraintValidator {
class CommentNameConstraintValidator extends ConstraintValidator implements ContainerInjectionInterface {
/**
* User storage handler.
*
* @var \Drupal\user\UserStorageInterface
*/
protected $userStorage;
/**
* Constructs a new CommentNameConstraintValidator.
*
* @param \Drupal\user\UserStorageInterface $user_storage
* The user storage handler.
*/
public function __construct(UserStorageInterface $user_storage) {
$this->userStorage = $user_storage;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
return new static($container->get('entity.manager')->getStorage('user'));
}
/**
* {@inheritdoc}
......@@ -37,8 +64,7 @@ public function validate($items, Constraint $constraint) {
// Do not allow unauthenticated comment authors to use a name that is
// taken by a registered user.
if (isset($author_name) && $author_name !== '' && $comment->getOwnerId() === 0) {
// @todo Properly inject dependency https://drupal.org/node/2197029
$users = \Drupal::entityManager()->getStorage('user')->loadByProperties(array('name' => $author_name));
$users = $this->userStorage->loadByProperties(array('name' => $author_name));
if (!empty($users)) {
$this->context->addViolation($constraint->messageNameTaken, array('%name' => $author_name));
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment