Commit cd661a19 authored by catch's avatar catch

Issue #2002168 by klausi, Github sync, effulgentsia: Convert form validation...

Issue #2002168 by klausi, Github sync, effulgentsia: Convert form validation of terms to entity validation.
parent e4e83f0e
......@@ -62,4 +62,20 @@ public static function schema(FieldDefinitionInterface $field_definition) {
);
}
/**
* {@inheritdoc}
*/
public function getConstraints() {
$constraints = parent::getConstraints();
if ($max_length = $this->getFieldSetting('max_length')) {
$constraint_manager = \Drupal::typedDataManager()->getValidationConstraintManager();
$constraints[] = $constraint_manager->create('ComplexData', array(
'value' => array('Length' => array('max' => $max_length))
));
}
return $constraints;
}
}
......@@ -8,20 +8,26 @@
namespace Drupal\Core\Field\Plugin\Field\FieldType;
use Drupal\Core\Field\FieldDefinitionInterface;
use Drupal\Core\Field\FieldItemBase;
use Drupal\Core\TypedData\DataDefinition;
/**
* Defines the 'uri' entity field type.
*
* URIs are not length limited by RFC 2616, but we need to provide a sensible
* default. There is a de-facto limit of 2000 characters in browsers and other
* implementors, so we go with 2048.
*
* @FieldType(
* id = "uri",
* label = @Translation("URI"),
* description = @Translation("An entity field containing a URI."),
* settings = {
* "max_length" = "2048"
* },
* configurable = FALSE
* )
*/
class UriItem extends FieldItemBase {
class UriItem extends StringItem {
/**
* Field definitions of the contained properties.
......
......@@ -16,12 +16,10 @@
* id = "uuid",
* label = @Translation("UUID"),
* description = @Translation("An entity field containing a UUID."),
* configurable = FALSE,
* constraints = {
* "ComplexData" = {
* "value" = {"Length" = {"max" = 128}}
* }
* }
* settings = {
* "max_length" = "128"
* },
* configurable = FALSE
* )
*/
class UuidItem extends StringItem {
......
......@@ -128,7 +128,7 @@ public static function baseFieldDefinitions($entity_type) {
->setLabel(t('Name'))
->setDescription(t('The name of the test entity.'))
->setTranslatable(TRUE)
->setPropertyConstraints('value', array('Length' => array('max' => 32)));
->setSetting('max_length', 32);
// @todo: Add allowed values validation.
$fields['type'] = FieldDefinition::create('string')
......
......@@ -208,6 +208,8 @@ public static function baseFieldDefinitions($entity_type) {
->setDescription(t('The term UUID.'))
->setReadOnly(TRUE);
// @todo Convert this to an entity_reference field, see
// https://drupal.org/node/2181593
$fields['vid'] = FieldDefinition::create('string')
->setLabel(t('Vocabulary ID'))
->setDescription(t('The ID of the vocabulary to which the term is assigned.'));
......@@ -218,7 +220,9 @@ public static function baseFieldDefinitions($entity_type) {
$fields['name'] = FieldDefinition::create('string')
->setLabel(t('Name'))
->setDescription(t('The term name.'));
->setDescription(t('The term name.'))
->setRequired(TRUE)
->setSetting('max_length', 255);
$fields['description'] = FieldDefinition::create('text_long')
->setLabel(t('Description'))
......@@ -230,12 +234,14 @@ public static function baseFieldDefinitions($entity_type) {
->setDescription(t('The weight of this term in relation to other terms.'))
->setSetting('default_value', 0);
// @todo Convert this to an entity_reference field, see
// https://drupal.org/node/1915056
$fields['parent'] = FieldDefinition::create('integer')
->setLabel(t('Term Parents'))
->setDescription(t('The parents of this term.'))
// Save new terms with no parents by default.
->setSetting('default_value', 0)
->setComputed(TRUE);
->setConstraints(array('TermParent' => array()));
$fields['changed'] = FieldDefinition::create('integer')
->setLabel(t('Changed'))
......
<?php
/**
* @file
* Contains \Drupal\taxonomy\Plugin\Validation\Constraint\TermParentConstraint.
*/
namespace Drupal\taxonomy\Plugin\Validation\Constraint;
use Symfony\Component\Validator\Constraint;
use Drupal\Component\Annotation\Plugin;
use Drupal\Core\Annotation\Translation;
/**
* Checks if a value is a valid taxonomy term parent (term id or 0).
*
* @Plugin(
* id = "TermParent",
* label = @Translation("Term parent", context = "Validation")
* )
*/
class TermParentConstraint extends Constraint {
/**
* The default violation message.
*
* @var string
*/
public $message = '%id is not a valid parent for this term.';
}
<?php
/**
* @file
* Contains \Drupal\taxonomy\Plugin\Validation\Constraint\TermParentConstraintValidator.
*/
namespace Drupal\taxonomy\Plugin\Validation\Constraint;
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\ConstraintValidator;
/**
* Validates the TermParent constraint.
*/
class TermParentConstraintValidator extends ConstraintValidator {
/**
* {@inheritdoc}
*/
public function validate($field_item, Constraint $constraint) {
if ($field_item) {
$parent_term_id = $field_item->value;
// If a non-0 parent term id is specified, ensure it corresponds to a real
// term in the same vocabulary.
if ($parent_term_id && !\Drupal::entityManager()->getStorageController('taxonomy_term')->loadByProperties(array('tid' => $parent_term_id, 'vid' => $field_item->getEntity()->vid->value))) {
$this->context->addViolation($constraint->message, array('%id' => $parent_term_id));
}
}
}
}
<?php
/**
* @file
* Contains \Drupal\taxonomy\Tests\TermValidationTest.
*/
namespace Drupal\taxonomy\Tests;
use Drupal\system\Tests\Entity\EntityUnitTestBase;
/**
* Tests term validation constraints.
*/
class TermValidationTest extends EntityUnitTestBase {
/**
* Modules to enable.
*
* @var array
*/
public static $modules = array('taxonomy');
public static function getInfo() {
return array(
'name' => 'Term Validation',
'description' => 'Tests the term validation constraints.',
'group' => 'Taxonomy',
);
}
/**
* {@inheritdoc}
*/
public function setUp() {
parent::setUp();
$this->installSchema('taxonomy', array('taxonomy_term_data'));
}
/**
* Tests the term validation constraints.
*/
public function testValidation() {
$term = $this->entityManager->getStorageController('taxonomy_term')->create(array(
'name' => 'test',
'vid' => 'tags',
));
$violations = $term->validate();
$this->assertEqual(count($violations), 0, 'No violations when validating a default term.');
$term->set('name', $this->randomString(256));
$violations = $term->validate();
$this->assertEqual(count($violations), 1, 'Violation found when name is too long.');
$this->assertEqual($violations[0]->getPropertyPath(), 'name.0.value');
$this->assertEqual($violations[0]->getMessage(), t('This value is too long. It should have %limit characters or less.', array('%limit' => 255)));
$term->set('name', NULL);
$violations = $term->validate();
$this->assertEqual(count($violations), 1, 'Violation found when name is NULL.');
$this->assertEqual($violations[0]->getPropertyPath(), 'name');
$this->assertEqual($violations[0]->getMessage(), t('This value should not be null.'));
$term->set('name', 'test');
$term->set('parent', 9999);
$violations = $term->validate();
$this->assertEqual(count($violations), 1, 'Violation found when term parent is invalid.');
$this->assertEqual($violations[0]->getMessage(), format_string('%id is not a valid parent for this term.', array('%id' => 9999)));
}
}
......@@ -472,8 +472,7 @@ public static function baseFieldDefinitions($entity_type) {
// @todo Convert to a text field in https://drupal.org/node/1548204.
$fields['signature'] = FieldDefinition::create('string')
->setLabel(t('Signature'))
->setDescription(t('The signature of this user.'))
->setPropertyConstraints('value', array('Length' => array('max' => 255)));
->setDescription(t('The signature of this user.'));
$fields['signature_format'] = FieldDefinition::create('string')
->setLabel(t('Signature format'))
->setDescription(t('The signature format of this user.'));
......@@ -481,7 +480,7 @@ public static function baseFieldDefinitions($entity_type) {
$fields['timezone'] = FieldDefinition::create('string')
->setLabel(t('Timezone'))
->setDescription(t('The timezone of this user.'))
->setPropertyConstraints('value', array('Length' => array('max' => 32)));
->setSetting('max_length', 32);
$fields['status'] = FieldDefinition::create('boolean')
->setLabel(t('User status'))
......
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