diff --git a/core/modules/block/custom_block/custom_block.pages.inc b/core/modules/block/custom_block/custom_block.pages.inc index 9c802c4..eba88fb 100644 --- a/core/modules/block/custom_block/custom_block.pages.inc +++ b/core/modules/block/custom_block/custom_block.pages.inc @@ -99,6 +99,18 @@ function custom_block_delete_form($form, &$form_state, CustomBlock $block) { '#value' => $block->id(), ); + $instances = entity_load_multiple_by_properties('block', array('plugin' => 'custom_block:' . $block->uuid->value)); + if (!empty($instances)) { + $form['instances'] = array( + '#type' => 'value', + '#value' => $instances, + ); + $form['message'] = array( + '#type' => 'markup', + '#markup' => format_plural(count($instances), 'This will also remove 1 placed block instance.', 'This will also remove @count placed block instances.'), + ); + } + return confirm_form( $form, t('Are you sure you want to delete %label?', array('%label' => $block->label())), @@ -112,8 +124,11 @@ function custom_block_delete_form($form, &$form_state, CustomBlock $block) { * Form submission handler for custom_block_delete_form(). */ function custom_block_delete_form_submit($form, &$form_state) { - // @todo Delete all configured instances of the block. $block = $form_state['custom_block']; + $instances = $form_state['values']['instances']; + foreach ($instances as $instance) { + $instance->delete(); + } $block->delete(); drupal_set_message(t('Custom block %label has been deleted.', array('%label' => $block->label()))); 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 c989682..baf239e 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 @@ -102,4 +102,49 @@ public function testFailedBlockCreation() { $this->assertTrue(count($records) > 0, 'Transactions not supported, and rollback error logged to watchdog.'); } } + + /** + * Test deleting a block. + */ + public function testBlockDelete() { + // Create a block. + $edit = array(); + $langcode = Language::LANGCODE_NOT_SPECIFIED; + $edit['info'] = $this->randomName(8); + $body = $this->randomName(16); + $edit["block_body[$langcode][0][value]"] = $body; + $this->drupalPost('block/add/basic', $edit, t('Save')); + + // Place the block. + $instance = array( + 'machine_name' => drupal_strtolower($edit['info']), + 'settings[label]' => $edit['info'], + 'region' => 'sidebar_first', + ); + $this->drupalPost(NULL, $instance, t('Save block')); + + $block = custom_block_load(1); + + // Navigate to home page. + $this->drupalGet(''); + $this->assertText($body); + + // Delete the block. + $this->drupalGet('block/1/delete'); + $this->assertText(format_plural(1, 'This will also remove 1 placed block instance.', 'This will also remove @count placed block instance.')); + + $this->drupalPost(NULL, array(), 'Delete'); + $this->assertRaw(t('Custom block %name has been deleted.', array('%name' => $edit['info']))); + + // Create another block and force the plugin cache to flush. + $edit2 = array(); + $langcode = Language::LANGCODE_NOT_SPECIFIED; + $edit2['info'] = $this->randomName(8); + $body2 = $this->randomName(16); + $edit2["block_body[$langcode][0][value]"] = $body2; + $this->drupalPost('block/add/basic', $edit2, t('Save')); + + $this->assertNoRaw('Error message'); + } + }