Commit 3b2fd07b authored by alexpott's avatar alexpott
Browse files

Issue #2529892 by larowlan, stefan.r, Berdir, almaudoh: BlockContentForm uses...

Issue #2529892 by larowlan, stefan.r, Berdir, almaudoh: BlockContentForm uses form validation instead of a validation Constraint
parent 9ec06367
......@@ -220,20 +220,4 @@ public function save(array $form, FormStateInterface $form_state) {
}
}
/**
* {@inheritdoc}
*/
public function validateForm(array &$form, FormStateInterface $form_state) {
$entity = parent::validateForm($form, $form_state);
if ($entity->isNew()) {
$exists = $this->blockContentStorage->loadByProperties(array('info' => $form_state->getValue(['info', 0, 'value'])));
if (!empty($exists)) {
$form_state->setErrorByName('info', $this->t('A block with description %name already exists.', array(
'%name' => $form_state->getValue(array('info', 0, 'value')),
)));
}
}
return $entity;
}
}
......@@ -189,7 +189,9 @@ public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
'type' => 'string_textfield',
'weight' => -5,
))
->setDisplayConfigurable('form', TRUE);
->setDisplayConfigurable('form', TRUE)
->addConstraint('BlockContentInfo', []);
$fields['type'] = BaseFieldDefinition::create('entity_reference')
->setLabel(t('Block type'))
......
<?php
/**
* @file
* Contains \Drupal\block_content\Plugin\Validation\Constraint\BlockContentInfoConstraint.
*/
namespace Drupal\block_content\Plugin\Validation\Constraint;
use Symfony\Component\Validator\Constraint;
/**
* Supports validating custom block names.
*
* @Constraint(
* id = "BlockContentInfo",
* label = @Translation("Custom block name", context = "Validation")
* )
*/
class BlockContentInfoConstraint extends Constraint {
public $message = 'A block with description %value already exists.';
/**
* {@inheritdoc}
*/
public function validatedBy() {
return '\Drupal\Core\Validation\Plugin\Validation\Constraint\UniqueFieldValueValidator';
}
}
<?php
/**
* @file
* Contains \Drupal\block_content\Tests\BlockContentValidationTest.
*/
namespace Drupal\block_content\Tests;
/**
* Tests block content validation constraints.
*
* @group block_content
*/
class BlockContentValidationTest extends BlockContentTestBase {
/**
* Tests the block content validation constraints.
*/
public function testValidation() {
// Add a block.
$description = $this->randomMachineName();
$block = $this->createBlockContent($description, 'basic');
// Validate the block.
$violations = $block->validate();
// Make sure we have no violations.
$this->assertEqual(count($violations), 0);
// Save the block.
$block->save();
// Add another block with the same description.
$block = $this->createBlockContent($description, 'basic');
// Validate this block.
$violations = $block->validate();
// Make sure we have 1 violation.
$this->assertEqual(count($violations), 1);
// 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.', [
'%value' => $block->label(),
]));
}
}
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