Issue #2529892 by larowlan, stefan.r, Berdir, almaudoh: BlockContentForm uses form validation instead of a validation Constraint
parent
9ec0636794
commit
3b2fd07bcf
|
@ -220,20 +220,4 @@ class BlockContentForm extends ContentEntityForm {
|
|||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* {@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 @@ class BlockContent extends ContentEntityBase implements BlockContentInterface {
|
|||
'type' => 'string_textfield',
|
||||
'weight' => -5,
|
||||
))
|
||||
->setDisplayConfigurable('form', TRUE);
|
||||
->setDisplayConfigurable('form', TRUE)
|
||||
->addConstraint('BlockContentInfo', []);
|
||||
|
||||
|
||||
$fields['type'] = BaseFieldDefinition::create('entity_reference')
|
||||
->setLabel(t('Block type'))
|
||||
|
|
|
@ -0,0 +1,31 @@
|
|||
<?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';
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,45 @@
|
|||
<?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(),
|
||||
]));
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue