diff --git a/core/modules/block_content/src/BlockContentForm.php b/core/modules/block_content/src/BlockContentForm.php index ec267cc39311..088c4d7bf41d 100644 --- a/core/modules/block_content/src/BlockContentForm.php +++ b/core/modules/block_content/src/BlockContentForm.php @@ -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; - } - } diff --git a/core/modules/block_content/src/Entity/BlockContent.php b/core/modules/block_content/src/Entity/BlockContent.php index 4e186b93c878..e05923d814b3 100644 --- a/core/modules/block_content/src/Entity/BlockContent.php +++ b/core/modules/block_content/src/Entity/BlockContent.php @@ -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')) diff --git a/core/modules/block_content/src/Plugin/Validation/Constraint/BlockContentInfoConstraint.php b/core/modules/block_content/src/Plugin/Validation/Constraint/BlockContentInfoConstraint.php new file mode 100644 index 000000000000..def6d8d92485 --- /dev/null +++ b/core/modules/block_content/src/Plugin/Validation/Constraint/BlockContentInfoConstraint.php @@ -0,0 +1,31 @@ +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(), + ])); + } + +}