Chris@0: alterInfo('validation_constraint'); Chris@0: $this->setCacheBackend($cache_backend, 'validation_constraint_plugins'); Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: protected function getDiscovery() { Chris@0: if (!isset($this->discovery)) { Chris@0: $this->discovery = parent::getDiscovery(); Chris@0: $this->discovery = new StaticDiscoveryDecorator($this->discovery, [$this, 'registerDefinitions']); Chris@0: } Chris@0: return $this->discovery; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Creates a validation constraint. Chris@0: * Chris@0: * @param string $name Chris@0: * The name or plugin id of the constraint. Chris@0: * @param mixed $options Chris@0: * The options to pass to the constraint class. Required and supported Chris@0: * options depend on the constraint class. Chris@0: * Chris@0: * @return \Symfony\Component\Validator\Constraint Chris@0: * A validation constraint plugin. Chris@0: */ Chris@0: public function create($name, $options) { Chris@0: if (!is_array($options)) { Chris@0: // Plugins need an array as configuration, so make sure we have one. Chris@0: // The constraint classes support passing the options as part of the Chris@0: // 'value' key also. Chris@0: $options = isset($options) ? ['value' => $options] : []; Chris@0: } Chris@0: return $this->createInstance($name, $options); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Callback for registering definitions for constraints shipped with Symfony. Chris@0: * Chris@0: * @see ConstraintManager::__construct() Chris@0: */ Chris@0: public function registerDefinitions() { Chris@0: $this->getDiscovery()->setDefinition('Callback', [ Chris@0: 'label' => new TranslatableMarkup('Callback'), Chris@0: 'class' => '\Symfony\Component\Validator\Constraints\Callback', Chris@0: 'type' => FALSE, Chris@0: ]); Chris@0: $this->getDiscovery()->setDefinition('Blank', [ Chris@0: 'label' => new TranslatableMarkup('Blank'), Chris@0: 'class' => '\Symfony\Component\Validator\Constraints\Blank', Chris@0: 'type' => FALSE, Chris@0: ]); Chris@0: $this->getDiscovery()->setDefinition('NotBlank', [ Chris@0: 'label' => new TranslatableMarkup('Not blank'), Chris@0: 'class' => '\Symfony\Component\Validator\Constraints\NotBlank', Chris@0: 'type' => FALSE, Chris@0: ]); Chris@0: $this->getDiscovery()->setDefinition('Email', [ Chris@0: 'label' => new TranslatableMarkup('Email'), Chris@0: 'class' => '\Drupal\Core\Validation\Plugin\Validation\Constraint\EmailConstraint', Chris@0: 'type' => ['string'], Chris@0: ]); Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function processDefinition(&$definition, $plugin_id) { Chris@0: // Make sure 'type' is set and either an array or FALSE. Chris@0: if ($definition['type'] !== FALSE && !is_array($definition['type'])) { Chris@0: $definition['type'] = [$definition['type']]; Chris@0: } Chris@0: } Chris@0: Chris@0: /** Chris@0: * Returns a list of constraints that support the given type. Chris@0: * Chris@0: * @param string $type Chris@0: * The type to filter on. Chris@0: * Chris@0: * @return array Chris@0: * An array of constraint plugin definitions supporting the given type, Chris@0: * keyed by constraint name (plugin ID). Chris@0: */ Chris@0: public function getDefinitionsByType($type) { Chris@0: $definitions = []; Chris@0: foreach ($this->getDefinitions() as $plugin_id => $definition) { Chris@0: if ($definition['type'] === FALSE || in_array($type, $definition['type'])) { Chris@0: $definitions[$plugin_id] = $definition; Chris@0: } Chris@0: } Chris@0: return $definitions; Chris@0: } Chris@0: Chris@0: }