ValidatorManager.php in Clientside Validation 8.2
File
src/ValidatorManager.php
View source
<?php
namespace Drupal\clientside_validation;
use Drupal\Core\Plugin\DefaultPluginManager;
use Drupal\Core\Cache\CacheBackendInterface;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Form\FormStateInterface;
class ValidatorManager extends DefaultPluginManager implements ValidatorManagerInterface {
public function __construct(\Traversable $namespaces, CacheBackendInterface $cache_backend, ModuleHandlerInterface $module_handler) {
parent::__construct('Plugin/CvValidator', $namespaces, $module_handler, 'Drupal\\clientside_validation\\CvValidatorInterface', 'Drupal\\clientside_validation\\Annotation\\CvValidator');
$this
->alterInfo('clientside_validation_validator_info');
$this
->setCacheBackend($cache_backend, 'clientside_validation_validators');
}
public function getValidators(array $element, FormStateInterface $form_state) {
$instances = [];
$validators = $this
->getDefinitionsForElement($element, $form_state);
foreach ($validators as $validator) {
$instances[] = $this
->createInstance($validator['id']);
}
return $instances;
}
protected function getDefinitionsForElement(array $element, FormStateInterface $form_state) {
$validators = $this
->getDefinitions();
$element_validators = [];
foreach ($validators as $validator) {
if (isset($element['#type']) && in_array($element['#type'], $validator['supports']['types'])) {
$element_validators[$validator['id']] = $validator;
}
foreach ($validator['supports']['attributes'] as $attribute) {
if (isset($element['#' . $attribute]) || isset($element['#attributes'][$attribute])) {
$element_validators[$validator['id']] = $validator;
continue;
}
}
}
return $element_validators;
}
}