Same filename and directory in other branches
- 8.9.x core/lib/Drupal/Core/Validation/ConstraintManager.php
- 9 core/lib/Drupal/Core/Validation/ConstraintManager.php
Namespace
Drupal\Core\Validation
File
core/lib/Drupal/Core/Validation/ConstraintManager.php
View source
<?php
namespace Drupal\Core\Validation;
use Drupal\Component\Plugin\Discovery\StaticDiscoveryDecorator;
use Drupal\Core\Cache\CacheBackendInterface;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Plugin\DefaultPluginManager;
use Drupal\Core\StringTranslation\TranslatableMarkup;
use Drupal\Core\Validation\Attribute\Constraint;
use Drupal\Core\Validation\Plugin\Validation\Constraint\EmailConstraint;
use Symfony\Component\Validator\Constraints\Blank;
use Symfony\Component\Validator\Constraints\Callback;
use Symfony\Component\Validator\Constraints\Choice;
use Symfony\Component\Validator\Constraints\Image;
use Symfony\Component\Validator\Constraints\NotBlank;
class ConstraintManager extends DefaultPluginManager {
public function __construct(\Traversable $namespaces, CacheBackendInterface $cache_backend, ModuleHandlerInterface $module_handler) {
$this->factory = new ConstraintFactory($this);
parent::__construct('Plugin/Validation/Constraint', $namespaces, $module_handler, NULL, Constraint::class, 'Drupal\\Core\\Validation\\Annotation\\Constraint');
$this
->alterInfo('validation_constraint');
$this
->setCacheBackend($cache_backend, 'validation_constraint_plugins');
}
protected function getDiscovery() {
if (!isset($this->discovery)) {
$this->discovery = parent::getDiscovery();
$this->discovery = new StaticDiscoveryDecorator($this->discovery, [
$this,
'registerDefinitions',
]);
}
return $this->discovery;
}
public function create($name, $options) {
if (!is_array($options)) {
$options = isset($options) ? [
'value' => $options,
] : [];
}
return $this
->createInstance($name, $options);
}
public function registerDefinitions() {
$this
->getDiscovery()
->setDefinition('Callback', [
'label' => new TranslatableMarkup('Callback'),
'class' => Callback::class,
'type' => FALSE,
]);
$this
->getDiscovery()
->setDefinition('Blank', [
'label' => new TranslatableMarkup('Blank'),
'class' => Blank::class,
'type' => FALSE,
]);
$this
->getDiscovery()
->setDefinition('NotBlank', [
'label' => new TranslatableMarkup('Not blank'),
'class' => NotBlank::class,
'type' => FALSE,
]);
$this
->getDiscovery()
->setDefinition('Email', [
'label' => new TranslatableMarkup('Email'),
'class' => EmailConstraint::class,
'type' => [
'string',
],
]);
$this
->getDiscovery()
->setDefinition('Choice', [
'label' => new TranslatableMarkup('Choice'),
'class' => Choice::class,
'type' => FALSE,
]);
$this
->getDiscovery()
->setDefinition('Image', [
'label' => new TranslatableMarkup('Image'),
'class' => Image::class,
'type' => [
'string',
],
]);
}
public function processDefinition(&$definition, $plugin_id) {
if ($definition['type'] !== FALSE && !is_array($definition['type'])) {
$definition['type'] = [
$definition['type'],
];
}
}
public function getDefinitionsByType($type) {
$definitions = [];
foreach ($this
->getDefinitions() as $plugin_id => $definition) {
if ($definition['type'] === FALSE || in_array($type, $definition['type'])) {
$definitions[$plugin_id] = $definition;
}
}
return $definitions;
}
}
Classes