TypedDataManager::getDefaultConstraints

public TypedDataManager::getDefaultConstraints(DataDefinitionInterface $definition)

Gets default constraints for the given data definition.

This generates default constraint definitions based on the data definition; for example, a NotNull constraint is generated if the data is defined as required. Besides that, any constraints defined for the data type (that is, below the 'constraint' key of the type's plugin definition) are taken into account.

Parameters

\Drupal\Core\TypedData\DataDefinitionInterface $definition: A data definition.

Return value

array An array of validation constraint definitions, keyed by constraint name. Each constraint definition can be used for instantiating \Symfony\Component\Validator\Constraint objects.

Overrides TypedDataManagerInterface::getDefaultConstraints

File

core/lib/Drupal/Core/TypedData/TypedDataManager.php, line 241

Class

TypedDataManager
Manages data type plugins.

Namespace

Drupal\Core\TypedData

Code

public function getDefaultConstraints(DataDefinitionInterface $definition) {
  $constraints = array();
  $type_definition = $this->getDefinition($definition->getDataType());
  // Auto-generate a constraint for data types implementing a primitive
  // interface.
  if (is_subclass_of($type_definition['class'], '\Drupal\Core\TypedData\PrimitiveInterface')) {
    $constraints['PrimitiveType'] = array();
  }
  // Add in constraints specified by the data type.
  if (isset($type_definition['constraints'])) {
    $constraints += $type_definition['constraints'];
  }
  // Add the NotNull constraint for required data.
  if ($definition->isRequired()) {
    $constraints['NotNull'] = array();
  }
  // Check if the class provides allowed values.
  if (is_subclass_of($definition->getClass(), 'Drupal\Core\TypedData\OptionsProviderInterface')) {
    $constraints['AllowedValues'] = array();
  }
  return $constraints;
}
doc_Drupal
2016-10-29 09:50:08
Comments
Leave a Comment

Please login to continue.