commit 217525be892961b955ccbbf832a48848210458df Author: fago Date: Sat May 25 12:34:59 2013 -0700 Implemented property_constraints. diff --git a/core/lib/Drupal/Core/Entity/EntityStorageControllerInterface.php b/core/lib/Drupal/Core/Entity/EntityStorageControllerInterface.php index 377a002..e189248 100644 --- a/core/lib/Drupal/Core/Entity/EntityStorageControllerInterface.php +++ b/core/lib/Drupal/Core/Entity/EntityStorageControllerInterface.php @@ -153,6 +153,14 @@ public function save(EntityInterface $entity); * - translatable: Whether the field is translatable. Defaults to FALSE. * - configurable: A boolean indicating whether the field is configurable * via field.module. Defaults to FALSE. + * - property_constraints: An array of constraint arrays applying to the + * field item properties, keyed by property name. E.g. the following + * validates the value property to have a maximum length of 128: + * @code + * array( + * 'value' => array('Length' => array('max' => 128)), + * ) + * @endcode * * @see Drupal\Core\TypedData\TypedDataManager::create() * @see typed_data() diff --git a/core/lib/Drupal/Core/Entity/Field/FieldItemBase.php b/core/lib/Drupal/Core/Entity/Field/FieldItemBase.php index 2c3feef..1c9eced 100644 --- a/core/lib/Drupal/Core/Entity/Field/FieldItemBase.php +++ b/core/lib/Drupal/Core/Entity/Field/FieldItemBase.php @@ -136,4 +136,18 @@ public function onChange($property_name) { // updated property object. unset($this->values[$property_name]); } + + /** + * {@inheritdoc} + */ + public function getConstraints() { + $constraints = parent::getConstraints(); + // If property constraints are present add in a ComplexData constraint for + // applying them. + if (!empty($this->definition['property_constraints'])) { + $constraints[] = \Drupal::typedData()->getValidationConstraintManager() + ->create('ComplexData', $this->definition['property_constraints']); + } + return $constraints; + } } \ No newline at end of file diff --git a/core/modules/system/lib/Drupal/system/Tests/Entity/EntityValidationTest.php b/core/modules/system/lib/Drupal/system/Tests/Entity/EntityValidationTest.php index bf02cdd..df430f2 100644 --- a/core/modules/system/lib/Drupal/system/Tests/Entity/EntityValidationTest.php +++ b/core/modules/system/lib/Drupal/system/Tests/Entity/EntityValidationTest.php @@ -80,7 +80,7 @@ protected function createTestEntity($entity_type) { public function testValidation() { // All entity variations have to have the same results. foreach (entity_test_entity_types() as $entity_type) { - $this->assertValidation($entity_type); + $this->checkValidation($entity_type); } } @@ -90,7 +90,7 @@ public function testValidation() { * @param string $entity_type * The entity type to run the tests with. */ - protected function assertValidation($entity_type) { + protected function checkValidation($entity_type) { $entity = $this->createTestEntity($entity_type); $violations = $entity->validate(); $this->assertEqual($violations->count(), 0, 'Validation passes.'); @@ -99,6 +99,14 @@ protected function assertValidation($entity_type) { $test_entity = clone $entity; $test_entity->uuid->value = $this->randomString(150); $this->assertEqual($test_entity->validate()->count(), 1, 'Validation failed.'); + + $test_entity = clone $entity; + $test_entity->langcode->value = $this->randomString(40); + $this->assertEqual($test_entity->validate()->count(), 1, 'Validation failed.'); + + $test_entity = clone $entity; + $test_entity->name->value = $this->randomString(40); + $this->assertEqual($test_entity->validate()->count(), 1, 'Validation failed.'); } } diff --git a/core/modules/system/tests/modules/entity_test/lib/Drupal/entity_test/EntityTestStorageController.php b/core/modules/system/tests/modules/entity_test/lib/Drupal/entity_test/EntityTestStorageController.php index 03543c9..2cf25ec 100644 --- a/core/modules/system/tests/modules/entity_test/lib/Drupal/entity_test/EntityTestStorageController.php +++ b/core/modules/system/tests/modules/entity_test/lib/Drupal/entity_test/EntityTestStorageController.php @@ -41,26 +41,16 @@ public function baseFieldDefinitions() { 'label' => t('UUID'), 'description' => t('The UUID of the test entity.'), 'type' => 'string_field', - 'constraints' => array( - 'ComplexData' => array( - 'value' => array( - 'Length' => array('max' => 128), - ), - ), - ), - // @todo: Support that. 'property_constraints' => array( - 'value' => array( - 'Length' => array('max' => 128), - ), + 'value' => array('Length' => array('max' => 128)), ), ); $fields['langcode'] = array( 'label' => t('Language code'), 'description' => t('The language code of the test entity.'), 'type' => 'language_field', - 'constraints' => array( - // 'Length' => array('max' => 12), + 'property_constraints' => array( + 'value' => array('Length' => array('max' => 12)), ), ); $fields['name'] = array( @@ -68,8 +58,8 @@ public function baseFieldDefinitions() { 'description' => t('The name of the test entity.'), 'type' => 'string_field', 'translatable' => TRUE, - 'constraints' => array( - // 'Length' => array('max' => 32), + 'property_constraints' => array( + 'value' => array('Length' => array('max' => 32)), ), ); $fields['type'] = array(