Constraint plugin manager.
Manages validation constraints based upon \Symfony\Component\Validator\Constraint, whereas Symfony constraints are added in manually during construction. Constraint options are passed on as plugin configuration during plugin instantiation.
While core does not prefix constraint plugins, modules have to prefix them with the module name in order to avoid any naming conflicts. E.g. a "profile" module would have to prefix any constraints with "Profile".
Constraint plugins may specify data types to which support is limited via the 'type' key of plugin definitions. Valid values are any types registered via the typed data API, or an array of multiple type names. For supporting all types FALSE may be specified. The key defaults to an empty array, i.e. no types are supported.
Expanded class hierarchy of ConstraintManager
class ConstraintManager extends PluginManagerBase {
/**
* Overrides \Drupal\Component\Plugin\PluginManagerBase::__construct().
*
* @param \Traversable $namespaces
* An object that implements \Traversable which contains the root paths
* keyed by the corresponding namespace to look for plugin implementations,
*/
public function __construct(\Traversable $namespaces) {
$this->discovery = new AnnotatedClassDiscovery('Validation/Constraint', $namespaces);
$this->discovery = new StaticDiscoveryDecorator($this->discovery, array(
$this,
'registerDefinitions',
));
$this->discovery = new DerivativeDiscoveryDecorator($this->discovery);
$this->discovery = new ProcessDecorator($this->discovery, array(
$this,
'processDefinition',
));
$this->discovery = new AlterDecorator($this->discovery, 'validation_constraint');
$this->discovery = new CacheDecorator($this->discovery, 'validation_constraints:' . language(Language::TYPE_INTERFACE)->langcode);
$this->factory = new DefaultFactory($this);
}
/**
* Creates a validation constraint.
*
* @param string $name
* The name or plugin id of the constraint.
* @param mixed $options
* The options to pass to the constraint class. Required and supported
* options depend on the constraint class.
*
* @return \Symfony\Component\Validator\Constraint
* A validation constraint plugin.
*/
public function create($name, $options) {
if (!is_array($options)) {
// Plugins need an array as configuration, so make sure we have one.
// The constraint classes support passing the options as part of the
// 'value' key also.
$options = array(
'value' => $options,
);
}
return $this
->createInstance($name, $options);
}
/**
* Callback for registering definitions for constraints shipped with Symfony.
*
* @see ConstraintManager::__construct()
*/
public function registerDefinitions() {
$this->discovery
->setDefinition('Null', array(
'label' => t('Null'),
'class' => '\\Symfony\\Component\\Validator\\Constraints\\Null',
'type' => FALSE,
));
$this->discovery
->setDefinition('NotNull', array(
'label' => t('Not null'),
'class' => '\\Symfony\\Component\\Validator\\Constraints\\NotNull',
'type' => FALSE,
));
$this->discovery
->setDefinition('Blank', array(
'label' => t('Blank'),
'class' => '\\Symfony\\Component\\Validator\\Constraints\\Blank',
'type' => FALSE,
));
$this->discovery
->setDefinition('NotBlank', array(
'label' => t('Not blank'),
'class' => '\\Symfony\\Component\\Validator\\Constraints\\NotBlank',
'type' => FALSE,
));
$this->discovery
->setDefinition('Email', array(
'label' => t('E-mail'),
'class' => '\\Symfony\\Component\\Validator\\Constraints\\Email',
'type' => array(
'string',
),
));
}
/**
* Process definition callback for the ProcessDecorator.
*/
public function processDefinition(&$definition, $plugin_id) {
// Make sure 'type' is set and either an array or FALSE.
if (!isset($definition['type'])) {
$definition['type'] = array();
}
elseif ($definition['type'] !== FALSE && !is_array($definition['type'])) {
$definition['type'] = array(
$definition['type'],
);
}
}
/**
* Returns a list of constraints that support the given type.
*
* @param string $type
* The type to filter on.
*
* @return array
* An array of constraint plugin definitions supporting the given type,
* keyed by constraint name (plugin ID).
*/
public function getDefinitionsByType($type) {
$definitions = array();
foreach ($this
->getDefinitions() as $plugin_id => $definition) {
if ($definition['type'] === FALSE || in_array($type, $definition['type'])) {
$definitions[$plugin_id] = $definition;
}
}
return $definitions;
}
}
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
ConstraintManager:: |
public | function | Creates a validation constraint. | |
ConstraintManager:: |
public | function | Returns a list of constraints that support the given type. | |
ConstraintManager:: |
public | function |
Process definition callback for the ProcessDecorator. Overrides PluginManagerBase:: |
|
ConstraintManager:: |
public | function | Callback for registering definitions for constraints shipped with Symfony. | |
ConstraintManager:: |
public | function | Overrides \Drupal\Component\Plugin\PluginManagerBase::__construct(). | |
PluginManagerBase:: |
protected | property | A set of defaults to be referenced by $this->processDefinition() if additional processing of plugins is necessary or helpful for development purposes. | 3 |
PluginManagerBase:: |
protected | property | The object that discovers plugins managed by this manager. | |
PluginManagerBase:: |
protected | property | The object that instantiates plugins managed by this manager. | |
PluginManagerBase:: |
protected | property | The object that returns the preconfigured plugin instance appropriate for a particular runtime condition. | |
PluginManagerBase:: |
public | function |
Clears static and persistent plugin definition caches. Overrides CachedDiscoveryInterface:: |
|
PluginManagerBase:: |
public | function |
Returns a preconfigured instance of a plugin. Overrides FactoryInterface:: |
6 |
PluginManagerBase:: |
public | function |
Gets a specific plugin definition. Overrides DiscoveryInterface:: |
|
PluginManagerBase:: |
public | function |
Gets the definition of all plugins for this type. Overrides DiscoveryInterface:: |
|
PluginManagerBase:: |
public | function |
Returns a preconfigured instance of a plugin. Overrides MapperInterface:: |
6 |