Commit 3bbaaa9b authored by catch's avatar catch

Issue #2116341 by fago, tim.plunkett: Apply defaults to definition objects.

parent 78d18c8d
......@@ -101,7 +101,7 @@ public function getDataType() {
* {@inheritdoc}
*/
public function getEntityTypeId() {
return $this->getConstraint('EntityType');
return isset($this->definition['constraints']['EntityType']) ? $this->definition['constraints']['EntityType'] : NULL;
}
/**
......@@ -115,7 +115,7 @@ public function setEntityTypeId($entity_type_id) {
* {@inheritdoc}
*/
public function getBundles() {
$bundle = $this->getConstraint('Bundle');
$bundle = isset($this->definition['constraints']['Bundle']) ? $this->definition['constraints']['Bundle'] : NULL;
return is_string($bundle) ? array($bundle) : $bundle;
}
......@@ -128,9 +128,7 @@ public function setBundles(array $bundles = NULL) {
}
else {
// Remove the constraint.
$constraints = $this->getConstraints();
unset($constraints['Bundle']);
$this->setConstraints($constraints);
unset($this->definition['constraints']['Bundle']);
}
return $this;
}
......
......@@ -238,7 +238,8 @@ public function getColumns();
/**
* Returns an array of validation constraints.
*
* See \Drupal\Core\TypedData\TypedDataManager::getConstraints() for details.
* See \Drupal\Core\TypedData\DataDefinitionInterface::getConstraints() for
* details.
*
* @return array[]
* An array of validation constraint definitions, keyed by constraint name.
......@@ -252,7 +253,8 @@ public function getConstraints();
/**
* Returns a validation constraint.
*
* See \Drupal\Core\TypedData\TypedDataManager::getConstraints() for details.
* See \Drupal\Core\TypedData\DataDefinitionInterface::getConstraints() for
* details.
*
* @param string $constraint_name
* The name of the the constraint, i.e. its plugin id.
......
......@@ -191,7 +191,13 @@ public function setRequired($required) {
* {@inheritdoc}
*/
public function getClass() {
return isset($this->definition['class']) ? $this->definition['class'] : NULL;
if (isset($this->definition['class'])) {
return $this->definition['class'];
}
else {
$type_definition = \Drupal::typedDataManager()->getDefinition($this->getDataType());
return $type_definition['class'];
}
}
/**
......@@ -253,23 +259,20 @@ public function setSetting($setting_name, $value) {
}
/**
* Returns an array of validation constraints.
*
* See \Drupal\Core\TypedData\TypedDataManager::getConstraints() for details.
*
* @return array
* Array of constraints, each being an instance of
* \Symfony\Component\Validator\Constraint.
* {@inheritdoc}
*/
public function getConstraints() {
return isset($this->definition['constraints']) ? $this->definition['constraints'] : array();
$constraints = isset($this->definition['constraints']) ? $this->definition['constraints'] : array();
$constraints += \Drupal::typedDataManager()->getDefaultConstraints($this);
return $constraints;
}
/**
* {@inheritdoc}
*/
public function getConstraint($constraint_name) {
return isset($this->definition['constraints'][$constraint_name]) ? $this->definition['constraints'][$constraint_name] : NULL;
$constraints = $this->getConstraints();
return isset($constraints[$constraint_name]) ? $constraints[$constraint_name] : NULL;
}
/**
......@@ -295,7 +298,8 @@ public function setConstraints(array $constraints) {
/**
* Adds a validation constraint.
*
* See \Drupal\Core\TypedData\TypedDataManager::getConstraints() for details.
* See \Drupal\Core\TypedData\DataDefinitionInterface::getConstraints() for
* details.
*
* @param string $constraint_name
* The name of the constraint to add, i.e. its plugin id.
......
......@@ -115,9 +115,9 @@ public function isRequired();
/**
* Returns the class used for creating the typed data object.
*
* If not specified, the default class of the data type will be used.
* If not specified, the default class of the data type will be returned.
*
* @return string|null
* @return string
* The class used for creating the typed data object.
*/
public function getClass();
......@@ -146,19 +146,49 @@ public function getSetting($setting_name);
/**
* Returns an array of validation constraints.
*
* See \Drupal\Core\TypedData\TypedDataManager::getConstraints() for details.
* The validation constraints of a definition consist of any for it defined
* constraints and default constraints, which are generated based on the
* definition and its data type. See
* \Drupal\Core\TypedData\TypedDataManager::getDefaultConstraints().
*
* @return array
* Constraints are defined via an array, having constraint plugin IDs as key
* and constraint options as values, e.g.
* @code
* $constraints = array(
* 'Range' => array('min' => 5, 'max' => 10),
* 'NotBlank' => array(),
* );
* @endcode
* Options have to be specified using another array if the constraint has more
* than one or zero options. If it has exactly one option, the value should be
* specified without nesting it into another array:
* @code
* $constraints = array(
* 'EntityType' => 'node',
* 'Bundle' => 'article',
* );
* @endcode
*
* Note that the specified constraints must be compatible with the data type,
* e.g. for data of type 'entity' the 'EntityType' and 'Bundle' constraints
* may be specified.
*
* @see \Drupal\Core\Validation\ConstraintManager
*
* @return array[]
* An array of validation constraint definitions, keyed by constraint name.
* Each constraint definition can be used for instantiating
* \Symfony\Component\Validator\Constraint objects.
*
* @see \Symfony\Component\Validator\Constraint
*/
public function getConstraints();
/**
* Returns a validation constraint.
*
* See \Drupal\Core\TypedData\TypedDataManager::getConstraints() for details.
* See \Drupal\Core\TypedData\DataDefinitionInterface::getConstraints() for
* details.
*
* @param string $constraint_name
* The name of the the constraint, i.e. its plugin id.
......@@ -166,6 +196,8 @@ public function getConstraints();
* @return array
* A validation constraint definition which can be used for instantiating a
* \Symfony\Component\Validator\Constraint object.
*
* @see \Symfony\Component\Validator\Constraint
*/
public function getConstraint($constraint_name);
......
......@@ -113,7 +113,12 @@ public function getString() {
*/
public function getConstraints() {
// @todo: Add the typed data manager as proper dependency.
return \Drupal::typedDataManager()->getConstraints($this->definition);
$constraint_manager = \Drupal::typedDataManager()->getValidationConstraintManager();
$constraints = array();
foreach ($this->definition->getConstraints() as $name => $options) {
$constraints[] = $constraint_manager->create($name, $options);
}
return $constraints;
}
/**
......
......@@ -94,7 +94,6 @@ public function createInstance($data_type, array $configuration = array()) {
// Allow per-data definition overrides of the used classes, i.e. take over
// classes specified in the type definition.
$class = $data_definition->getClass();
$class = isset($class) ? $class : $type_definition['class'];
if (!isset($class)) {
throw new PluginException(sprintf('The plugin (%s) did not specify an instance class.', $data_type));
......@@ -349,88 +348,45 @@ public function getValidationConstraintManager() {
}
/**
* Gets configured constraints from a data definition.
* Gets default constraints for the given data definition.
*
* Any constraints defined for the data type, i.e. below the 'constraint' key
* of the type's plugin definition, or constraints defined below the data
* definition's constraint' key are taken into account.
*
* Constraints are defined via an array, having constraint plugin IDs as key
* and constraint options as values, e.g.
* @code
* $constraints = array(
* 'Range' => array('min' => 5, 'max' => 10),
* 'NotBlank' => array(),
* );
* @endcode
* Options have to be specified using another array if the constraint has more
* than one or zero options. If it has exactly one option, the value should be
* specified without nesting it into another array:
* @code
* $constraints = array(
* 'EntityType' => 'node',
* 'Bundle' => 'article',
* );
* @endcode
*
* Note that the specified constraints must be compatible with the data type,
* e.g. for data of type 'entity' the 'EntityType' and 'Bundle' constraints
* may be specified.
*
* @see \Drupal\Core\Validation\ConstraintManager
* This generates default constraint definitions based on the data definition;
* e.g. a NotNull constraint is generated if the data is defined as required.
* Besides that any constraints defined for the data type, i.e. below the
* 'constraint' key of the type's plugin definition, are taken into account.
*
* @param \Drupal\Core\TypedData\DataDefinitionInterface $definition
* A data definition.
*
* @return array
* Array of constraints, each being an instance of
* \Symfony\Component\Validator\Constraint.
*
* @todo: Having this as well as $definition->getConstraints() is confusing.
* An array of validation constraint definitions, keyed by constraint name.
* Each constraint definition can be used for instantiating
* \Symfony\Component\Validator\Constraint objects.
*/
public function getConstraints(DataDefinitionInterface $definition) {
public function getDefaultConstraints(DataDefinitionInterface $definition) {
$constraints = array();
$validation_manager = $this->getValidationConstraintManager();
$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[] = $validation_manager->create('PrimitiveType', array());
$constraints['PrimitiveType'] = array();
}
// Add in constraints specified by the data type.
if (isset($type_definition['constraints'])) {
foreach ($type_definition['constraints'] as $name => $options) {
$constraints[] = $validation_manager->create($name, $options);
}
}
// Add any constraints specified as part of the data definition.
$defined_constraints = $definition->getConstraints();
foreach ($defined_constraints as $name => $options) {
$constraints[] = $validation_manager->create($name, $options);
$constraints += $type_definition['constraints'];
}
// Add the NotNull constraint for required data.
if ($definition->isRequired() && !isset($defined_constraints['NotNull'])) {
$constraints[] = $validation_manager->create('NotNull', array());
}
// If the definition does not provide a class use the class from the type
// definition for performing interface checks.
$class = $definition->getClass();
if (!$class) {
$class = $type_definition['class'];
if ($definition->isRequired()) {
$constraints['NotNull'] = array();
}
// Check if the class provides allowed values.
if (is_subclass_of($class,'Drupal\Core\TypedData\AllowedValuesInterface')) {
$constraints[] = $validation_manager->create('AllowedValues', array());
if (is_subclass_of($definition->getClass(),'Drupal\Core\TypedData\AllowedValuesInterface')) {
$constraints['AllowedValues'] = array();
}
// Add any constraints about referenced data.
if ($definition instanceof DataReferenceDefinitionInterface) {
foreach ($definition->getTargetDefinition()->getConstraints() as $name => $options) {
$constraints[] = $validation_manager->create($name, $options);
}
$constraints += $definition->getTargetDefinition()->getConstraints();
}
return $constraints;
}
......
......@@ -726,14 +726,15 @@ public function getClass() {
* {@inheritdoc}
*/
public function getConstraints() {
return array();
return \Drupal::typedDataManager()->getDefaultConstraints($this);
}
/**
* {@inheritdoc}
*/
public function getConstraint($constraint_name) {
return NULL;
$constraints = $this->getConstraints();
return isset($constraints[$constraint_name]) ? $constraints[$constraint_name] : NULL;
}
/**
......
......@@ -35,7 +35,7 @@ class TextFieldRdfaTest extends FieldRdfaTestBase {
/**
* {@inheritdoc}
*/
public static $modules = array('text');
public static $modules = array('text', 'filter');
public static function getInfo() {
return array(
......
......@@ -18,6 +18,7 @@
use Drupal\Core\TypedData\Type\StringInterface;
use Drupal\Core\TypedData\TypedDataInterface;
use Drupal\field\Entity\FieldInstanceConfig;
use Drupal\node\Entity\NodeType;
/**
* Tests Entity API base functionality.
......@@ -578,6 +579,8 @@ public function testEntityConstraintValidation() {
$this->assertEqual($violations->count(), 1);
// Test bundle validation.
NodeType::create(array('type' => 'article'))
->save();
$definition = FieldDefinition::create('entity_reference')
->setLabel('Test entity')
->setSettings(array(
......
......@@ -1079,13 +1079,6 @@ public function getString() {
return $this->storage->getString();
}
/**
* Implements \Drupal\Core\TypedData\TypedDataInterface::getConstraints().
*/
public function getConstraints() {
return $this->storage->getConstraints();
}
/**
* Implements \Drupal\Core\TypedData\TypedDataInterface::validate().
*/
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment