Commit c73fdfce authored by alexpott's avatar alexpott

Issue #2483869 by almaudoh, stefan.r, swentel, Berdir: Make Unique Field constraints generic

parent c2563f6b
......@@ -226,7 +226,7 @@ public function addPropertyConstraints($name, array $constraints);
* @code
* // Add a constraint to the 'field_username' FieldItemList.
* // e.g. $node->field_username
* $fields['field_username']->addConstraint('UserNameUnique', []);
* $fields['field_username']->addConstraint('UniqueField');
* @endcode
*
* If you wish to apply a constraint to a \Drupal\Core\Field\FieldItem instead
......
......@@ -2,24 +2,24 @@
/**
* @file
* Contains \Drupal\block_content\Plugin\Validation\Constraint\BlockContentInfoConstraint.
* Contains \Drupal\Core\Validation\Plugin\Validation\Constraint\UniqueFieldConstraint.
*/
namespace Drupal\block_content\Plugin\Validation\Constraint;
namespace Drupal\Core\Validation\Plugin\Validation\Constraint;
use Symfony\Component\Validator\Constraint;
/**
* Supports validating custom block names.
* Checks if an entity field has a unique value.
*
* @Constraint(
* id = "BlockContentInfo",
* label = @Translation("Custom block name", context = "Validation")
* id = "UniqueField",
* label = @Translation("Unique field constraint", context = "Validation"),
* )
*/
class BlockContentInfoConstraint extends Constraint {
class UniqueFieldConstraint extends Constraint {
public $message = 'A block with description %value already exists.';
public $message = 'A @entity_type with @field_name %value already exists.';
/**
* {@inheritdoc}
......
......@@ -7,6 +7,7 @@
namespace Drupal\Core\Validation\Plugin\Validation\Constraint;
use Drupal\Component\Utility\Unicode;
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\ConstraintValidator;
......@@ -37,7 +38,11 @@ public function validate($items, Constraint $constraint) {
->execute();
if ($value_taken) {
$this->context->addViolation($constraint->message, array("%value" => $item->value));
$this->context->addViolation($constraint->message, [
'%value' => $item->value,
'@entity_type' => $entity->getEntityType()->getLowercaseLabel(),
'@field_name' => Unicode::strtolower($items->getFieldDefinition()->getLabel()),
]);
}
}
}
......@@ -151,7 +151,7 @@ public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
'weight' => -5,
))
->setDisplayConfigurable('form', TRUE)
->addConstraint('FeedTitle', []);
->addConstraint('FeedTitle');
$fields['langcode'] = BaseFieldDefinition::create('language')
->setLabel(t('Language code'))
......@@ -173,7 +173,7 @@ public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
'weight' => -3,
))
->setDisplayConfigurable('form', TRUE)
->addConstraint('FeedUrl', []);
->addConstraint('FeedUrl');
$intervals = array(900, 1800, 3600, 7200, 10800, 21600, 32400, 43200, 64800, 86400, 172800, 259200, 604800, 1209600, 2419200);
$period = array_map(array(\Drupal::service('date.formatter'), 'formatInterval'), array_combine($intervals, $intervals));
......
......@@ -7,7 +7,7 @@
namespace Drupal\aggregator\Plugin\Validation\Constraint;
use Symfony\Component\Validator\Constraint;
use Drupal\Core\Validation\Plugin\Validation\Constraint\UniqueFieldConstraint;
/**
* Supports validating feed titles.
......@@ -17,15 +17,8 @@
* label = @Translation("Feed title", context = "Validation")
* )
*/
class FeedTitleConstraint extends Constraint {
class FeedTitleConstraint extends UniqueFieldConstraint {
public $message = 'A feed named %value already exists. Enter a unique title.';
/**
* {@inheritdoc}
*/
public function validatedBy() {
return '\Drupal\Core\Validation\Plugin\Validation\Constraint\UniqueFieldValueValidator';
}
}
......@@ -7,7 +7,7 @@
namespace Drupal\aggregator\Plugin\Validation\Constraint;
use Symfony\Component\Validator\Constraint;
use Drupal\Core\Validation\Plugin\Validation\Constraint\UniqueFieldConstraint;
/**
* Supports validating feed URLs.
......@@ -17,15 +17,8 @@
* label = @Translation("Feed URL", context = "Validation")
* )
*/
class FeedUrlConstraint extends Constraint {
class FeedUrlConstraint extends UniqueFieldConstraint {
public $message = 'A feed with this URL %value already exists. Enter a unique URL.';
/**
* {@inheritdoc}
*/
public function validatedBy() {
return '\Drupal\Core\Validation\Plugin\Validation\Constraint\UniqueFieldValueValidator';
}
}
......@@ -190,7 +190,7 @@ public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
'weight' => -5,
))
->setDisplayConfigurable('form', TRUE)
->addConstraint('BlockContentInfo', []);
->addConstraint('UniqueField', []);
$fields['type'] = BaseFieldDefinition::create('entity_reference')
......
......@@ -77,8 +77,8 @@ public function testBlockContentCreation() {
$this->drupalPostForm('block/add/basic', $edit, t('Save'));
// Check that the Basic block has been created.
$this->assertRaw(format_string('A block with description %name already exists.', array(
'%name' => $edit['info[0][value]']
$this->assertRaw(format_string('A custom block with block description %value already exists.', array(
'%value' => $edit['info[0][value]']
)));
$this->assertResponse(200);
}
......@@ -156,8 +156,8 @@ public function testBlockContentCreationMultipleViewModes() {
$this->drupalPostForm('block/add/basic', $edit, t('Save'));
// Check that the Basic block has been created.
$this->assertRaw(format_string('A block with description %name already exists.', array(
'%name' => $edit['info[0][value]']
$this->assertRaw(format_string('A custom block with block description %value already exists.', array(
'%value' => $edit['info[0][value]']
)));
$this->assertResponse(200);
}
......
......@@ -71,17 +71,20 @@ protected function setUp() {
* Defaults to FALSE.
* @param string $bundle
* (optional) Bundle name. Defaults to 'basic'.
* @param bool $save
* (optional) Whether to save the block. Defaults to TRUE.
*
* @return \Drupal\block_content\Entity\BlockContent
* Created custom block.
*/
protected function createBlockContent($title = FALSE, $bundle = 'basic') {
protected function createBlockContent($title = FALSE, $bundle = 'basic', $save = TRUE) {
$title = ($title ? : $this->randomMachineName());
if ($block_content = entity_create('block_content', array(
$block_content = entity_create('block_content', array(
'info' => $title,
'type' => $bundle,
'langcode' => 'en'
))) {
));
if ($block_content && $save === TRUE) {
$block_content->save();
}
return $block_content;
......
......@@ -37,7 +37,7 @@ public function testValidation() {
// Make sure the violation is on the info property
$this->assertEqual($violations[0]->getPropertyPath(), 'info');
// Make sure the message is correct.
$this->assertEqual($violations[0]->getMessage(), format_string('A block with description %value already exists.', [
$this->assertEqual($violations[0]->getMessage(), format_string('A custom block with block description %value already exists.', [
'%value' => $block->label(),
]));
}
......
......@@ -7,7 +7,7 @@
namespace Drupal\user\Plugin\Validation\Constraint;
use Symfony\Component\Validator\Constraint;
use Drupal\Core\Validation\Plugin\Validation\Constraint\UniqueFieldConstraint;
/**
* Checks if a user's email address is unique on the site.
......@@ -17,14 +17,8 @@
* label = @Translation("User email unique", context = "Validation")
* )
*/
class UserMailUnique extends Constraint {
class UserMailUnique extends UniqueFieldConstraint {
public $message = 'The email address %value is already taken.';
/**
* {@inheritdoc}
*/
public function validatedBy() {
return '\Drupal\Core\Validation\Plugin\Validation\Constraint\UniqueFieldValueValidator';
}
}
......@@ -7,7 +7,7 @@
namespace Drupal\user\Plugin\Validation\Constraint;
use Symfony\Component\Validator\Constraint;
use Drupal\Core\Validation\Plugin\Validation\Constraint\UniqueFieldConstraint;
/**
* Checks if a user name is unique on the site.
......@@ -17,14 +17,8 @@
* label = @Translation("User name unique", context = "Validation"),
* )
*/
class UserNameUnique extends Constraint {
class UserNameUnique extends UniqueFieldConstraint {
public $message = 'The username %value is already taken.';
/**
* {@inheritdoc}
*/
public function validatedBy() {
return '\Drupal\Core\Validation\Plugin\Validation\Constraint\UniqueFieldValueValidator';
}
}
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