From edcc75602fe1fc3b1d15049bdaec0198ce5aa43a Mon Sep 17 00:00:00 2001 From: webchick <webchick@24967.no-reply.drupal.org> Date: Sat, 10 Aug 2013 00:43:37 -0700 Subject: [PATCH] Issue #1998658 by larowlan: Fixed Creating Custom Block with same name as existing block throws SQL error. --- .../custom_block/CustomBlockFormController.php | 15 +++++++++++++++ .../Tests/CustomBlockCreationTest.php | 10 ++++++++++ 2 files changed, 25 insertions(+) diff --git a/core/modules/block/custom_block/lib/Drupal/custom_block/CustomBlockFormController.php b/core/modules/block/custom_block/lib/Drupal/custom_block/CustomBlockFormController.php index 9ccf37135006..cde78aeeedd6 100644 --- a/core/modules/block/custom_block/lib/Drupal/custom_block/CustomBlockFormController.php +++ b/core/modules/block/custom_block/lib/Drupal/custom_block/CustomBlockFormController.php @@ -219,4 +219,19 @@ public function delete(array $form, array &$form_state) { $form_state['redirect'] = array('block/' . $block->id() . '/delete', array('query' => $destination)); } + /** + * {@inheritdoc} + */ + public function validateForm(array &$form, array &$form_state) { + if ($this->entity->isNew()) { + // @todo Inject this once https://drupal.org/node/2060865 is in. + $exists = \Drupal::entityManager()->getStorageController('custom_block')->loadByProperties(array('info' => $form_state['values']['info'])); + if (!empty($exists)) { + form_set_error('info', t('A block with description %name already exists.', array( + '%name' => $form_state['values']['info'] + ))); + } + } + } + } diff --git a/core/modules/block/custom_block/lib/Drupal/custom_block/Tests/CustomBlockCreationTest.php b/core/modules/block/custom_block/lib/Drupal/custom_block/Tests/CustomBlockCreationTest.php index 8beda0bd4b50..4fe834764950 100644 --- a/core/modules/block/custom_block/lib/Drupal/custom_block/Tests/CustomBlockCreationTest.php +++ b/core/modules/block/custom_block/lib/Drupal/custom_block/Tests/CustomBlockCreationTest.php @@ -64,6 +64,16 @@ public function testCustomBlockCreation() { $blocks = entity_load_multiple_by_properties('custom_block', array('info' => $edit['info'])); $block = reset($blocks); $this->assertTrue($block, 'Custom Block found in database.'); + + // Check that attempting to create another block with the same value for + // 'info' returns an error. + $this->drupalPost('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"] + ))); + $this->assertResponse(200); } /** -- GitLab