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() { ...@@ -101,7 +101,7 @@ public function getDataType() {
* {@inheritdoc} * {@inheritdoc}
*/ */
public function getEntityTypeId() { 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) { ...@@ -115,7 +115,7 @@ public function setEntityTypeId($entity_type_id) {
* {@inheritdoc} * {@inheritdoc}
*/ */
public function getBundles() { 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; return is_string($bundle) ? array($bundle) : $bundle;
} }
...@@ -128,9 +128,7 @@ public function setBundles(array $bundles = NULL) { ...@@ -128,9 +128,7 @@ public function setBundles(array $bundles = NULL) {
} }
else { else {
// Remove the constraint. // Remove the constraint.
$constraints = $this->getConstraints(); unset($this->definition['constraints']['Bundle']);
unset($constraints['Bundle']);
$this->setConstraints($constraints);
} }
return $this; return $this;
} }
......
...@@ -238,7 +238,8 @@ public function getColumns(); ...@@ -238,7 +238,8 @@ public function getColumns();
/** /**
* Returns an array of validation constraints. * Returns an array of validation constraints.
* *
* See \Drupal\Core\TypedData\TypedDataManager::getConstraints() for details. * See \Drupal\Core\TypedData\DataDefinitionInterface::getConstraints() for
* details.
* *
* @return array[] * @return array[]
* An array of validation constraint definitions, keyed by constraint name. * An array of validation constraint definitions, keyed by constraint name.
...@@ -252,7 +253,8 @@ public function getConstraints(); ...@@ -252,7 +253,8 @@ public function getConstraints();
/** /**
* Returns a validation constraint. * Returns a validation constraint.
* *
* See \Drupal\Core\TypedData\TypedDataManager::getConstraints() for details. * See \Drupal\Core\TypedData\DataDefinitionInterface::getConstraints() for
* details.
* *
* @param string $constraint_name * @param string $constraint_name
* The name of the the constraint, i.e. its plugin id. * The name of the the constraint, i.e. its plugin id.
......
...@@ -191,7 +191,13 @@ public function setRequired($required) { ...@@ -191,7 +191,13 @@ public function setRequired($required) {
* {@inheritdoc} * {@inheritdoc}
*/ */
public function getClass() { 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) { ...@@ -253,23 +259,20 @@ public function setSetting($setting_name, $value) {
} }
/** /**
* Returns an array of validation constraints. * {@inheritdoc}
*
* See \Drupal\Core\TypedData\TypedDataManager::getConstraints() for details.
*
* @return array
* Array of constraints, each being an instance of
* \Symfony\Component\Validator\Constraint.
*/ */
public function getConstraints() { 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} * {@inheritdoc}
*/ */
public function getConstraint($constraint_name) { 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) { ...@@ -295,7 +298,8 @@ public function setConstraints(array $constraints) {
/** /**
* Adds a validation constraint. * Adds a validation constraint.
* *
* See \Drupal\Core\TypedData\TypedDataManager::getConstraints() for details. * See \Drupal\Core\TypedData\DataDefinitionInterface::getConstraints() for
* details.
* *
* @param string $constraint_name * @param string $constraint_name
* The name of the constraint to add, i.e. its plugin id. * The name of the constraint to add, i.e. its plugin id.
......
...@@ -115,9 +115,9 @@ public function isRequired(); ...@@ -115,9 +115,9 @@ public function isRequired();
/** /**
* Returns the class used for creating the typed data object. * 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. * The class used for creating the typed data object.
*/ */
public function getClass(); public function getClass();
...@@ -146,19 +146,49 @@ public function getSetting($setting_name); ...@@ -146,19 +146,49 @@ public function getSetting($setting_name);
/** /**
* Returns an array of validation constraints. * 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. * An array of validation constraint definitions, keyed by constraint name.
* Each constraint definition can be used for instantiating * Each constraint definition can be used for instantiating
* \Symfony\Component\Validator\Constraint objects. * \Symfony\Component\Validator\Constraint objects.
*
* @see \Symfony\Component\Validator\Constraint
*/ */
public function getConstraints(); public function getConstraints();
/** /**
* Returns a validation constraint. * Returns a validation constraint.
* *
* See \Drupal\Core\TypedData\TypedDataManager::getConstraints() for details. * See \Drupal\Core\TypedData\DataDefinitionInterface::getConstraints() for
* details.
* *
* @param string $constraint_name * @param string $constraint_name
* The name of the the constraint, i.e. its plugin id. * The name of the the constraint, i.e. its plugin id.
...@@ -166,6 +196,8 @@ public function getConstraints(); ...@@ -166,6 +196,8 @@ public function getConstraints();
* @return array * @return array
* A validation constraint definition which can be used for instantiating a * A validation constraint definition which can be used for instantiating a
* \Symfony\Component\Validator\Constraint object. * \Symfony\Component\Validator\Constraint object.
*
* @see \Symfony\Component\Validator\Constraint
*/ */
public function getConstraint($constraint_name); public function getConstraint($constraint_name);
......
...@@ -113,7 +113,12 @@ public function getString() { ...@@ -113,7 +113,12 @@ public function getString() {
*/ */
public function getConstraints() { public function getConstraints() {
// @todo: Add the typed data manager as proper dependency. // @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()) { ...@@ -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 // Allow per-data definition overrides of the used classes, i.e. take over
// classes specified in the type definition. // classes specified in the type definition.
$class = $data_definition->getClass(); $class = $data_definition->getClass();
$class = isset($class) ? $class : $type_definition['class'];
if (!isset($class)) { if (!isset($class)) {
throw new PluginException(sprintf('The plugin (%s) did not specify an instance class.', $data_type)); throw new PluginException(sprintf('The plugin (%s) did not specify an instance class.', $data_type));
...@@ -349,88 +348,45 @@ public function getValidationConstraintManager() { ...@@ -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 * This generates default constraint definitions based on the data definition;
* of the type's plugin definition, or constraints defined below the data * e.g. a NotNull constraint is generated if the data is defined as required.
* definition's constraint' key are taken into account. * 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.
* 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
* *
* @param \Drupal\Core\TypedData\DataDefinitionInterface $definition * @param \Drupal\Core\TypedData\DataDefinitionInterface $definition
* A data definition. * A data definition.
* *
* @return array * @return array
* Array of constraints, each being an instance of * An array of validation constraint definitions, keyed by constraint name.
* \Symfony\Component\Validator\Constraint. * Each constraint definition can be used for instantiating
* * \Symfony\Component\Validator\Constraint objects.
* @todo: Having this as well as $definition->getConstraints() is confusing.
*/ */
public function getConstraints(DataDefinitionInterface $definition) { public function getDefaultConstraints(DataDefinitionInterface $definition) {
$constraints = array(); $constraints = array();
$validation_manager = $this->getValidationConstraintManager();
$type_definition = $this->getDefinition($definition->getDataType()); $type_definition = $this->getDefinition($definition->getDataType());
// Auto-generate a constraint for data types implementing a primitive // Auto-generate a constraint for data types implementing a primitive
// interface. // interface.
if (is_subclass_of($type_definition['class'], '\Drupal\Core\TypedData\PrimitiveInterface')) { 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. // Add in constraints specified by the data type.
if (isset($type_definition['constraints'])) { if (isset($type_definition['constraints'])) {
foreach ($type_definition['constraints'] as $name => $options) { $constraints += $type_definition['constraints'];
$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);
} }
// Add the NotNull constraint for required data. // Add the NotNull constraint for required data.
if ($definition->isRequired() && !isset($defined_constraints['NotNull'])) { if ($definition->isRequired()) {
$constraints[] = $validation_manager->create('NotNull', array()); $constraints['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'];
} }
// Check if the class provides allowed values. // Check if the class provides allowed values.
if (is_subclass_of($class,'Drupal\Core\TypedData\AllowedValuesInterface')) { if (is_subclass_of($definition->getClass(),'Drupal\Core\TypedData\AllowedValuesInterface')) {
$constraints[] = $validation_manager->create('AllowedValues', array()); $constraints['AllowedValues'] = array();
} }
// Add any constraints about referenced data. // Add any constraints about referenced data.
if ($definition instanceof DataReferenceDefinitionInterface) { if ($definition instanceof DataReferenceDefinitionInterface) {
foreach ($definition->getTargetDefinition()->getConstraints() as $name => $options) { $constraints += $definition->getTargetDefinition()->getConstraints();
$constraints[] = $validation_manager->create($name, $options);
}
} }
return $constraints; return $constraints;
} }
......
...@@ -726,14 +726,15 @@ public function getClass() { ...@@ -726,14 +726,15 @@ public function getClass() {
* {@inheritdoc} * {@inheritdoc}
*/ */
public function getConstraints() { public function getConstraints() {
return array(); return \Drupal::typedDataManager()->getDefaultConstraints($this);
} }
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function getConstraint($constraint_name) { 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 { ...@@ -35,7 +35,7 @@ class TextFieldRdfaTest extends FieldRdfaTestBase {
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public static $modules = array('text'); public static $modules = array('text', 'filter');
public static function getInfo() { public static function getInfo() {
return array( return array(
......
...@@ -18,6 +18,7 @@ ...@@ -18,6 +18,7 @@
use Drupal\Core\TypedData\Type\StringInterface; use Drupal\Core\TypedData\Type\StringInterface;
use Drupal\Core\TypedData\TypedDataInterface; use Drupal\Core\TypedData\TypedDataInterface;
use Drupal\field\Entity\FieldInstanceConfig; use Drupal\field\Entity\FieldInstanceConfig;
use Drupal\node\Entity\NodeType;
/** /**
* Tests Entity API base functionality. * Tests Entity API base functionality.
...@@ -578,6 +579,8 @@ public function testEntityConstraintValidation() { ...@@ -578,6 +579,8 @@ public function testEntityConstraintValidation() {
$this->assertEqual($violations->count(), 1); $this->assertEqual($violations->count(), 1);
// Test bundle validation. // Test bundle validation.
NodeType::create(array('type' => 'article'))
->save();
$definition = FieldDefinition::create('entity_reference') $definition = FieldDefinition::create('entity_reference')
->setLabel('Test entity') ->setLabel('Test entity')
->setSettings(array( ->setSettings(array(
......
...@@ -1079,13 +1079,6 @@ public function getString() { ...@@ -1079,13 +1079,6 @@ public function getString() {
return $this->storage->getString(); return $this->storage->getString();
} }
/**
* Implements \Drupal\Core\TypedData\TypedDataInterface::getConstraints().
*/
public function getConstraints() {
return $this->storage->getConstraints();
}
/** /**
* Implements \Drupal\Core\TypedData\TypedDataInterface::validate(). * 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