diff --git a/core/modules/block/custom_block/custom_block.pages.inc b/core/modules/block/custom_block/custom_block.pages.inc index 297475c..a0dc773 100644 --- a/core/modules/block/custom_block/custom_block.pages.inc +++ b/core/modules/block/custom_block/custom_block.pages.inc @@ -92,6 +92,14 @@ function custom_block_delete_form($form, &$form_state, CustomBlock $block) { '#value' => $block->id(), ); + $instances = $block->getInstances(); + + $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.'), + '#access' => !empty($instances), + ); + return confirm_form( $form, t('Are you sure you want to delete %label?', array('%label' => $block->label())), @@ -105,7 +113,6 @@ 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']; $block->delete(); diff --git a/core/modules/block/custom_block/lib/Drupal/custom_block/CustomBlockInterface.php b/core/modules/block/custom_block/lib/Drupal/custom_block/CustomBlockInterface.php index e620031..e650389 100644 --- a/core/modules/block/custom_block/lib/Drupal/custom_block/CustomBlockInterface.php +++ b/core/modules/block/custom_block/lib/Drupal/custom_block/CustomBlockInterface.php @@ -38,4 +38,12 @@ public function setTheme($theme); */ public function getTheme(); + /** + * Gets the configured instances of this custom block. + * + * @return array + * Array of Drupal\block\Core\Plugin\Entity\Block entities. + */ + public function getInstances(); + } diff --git a/core/modules/block/custom_block/lib/Drupal/custom_block/Plugin/Core/Entity/CustomBlock.php b/core/modules/block/custom_block/lib/Drupal/custom_block/Plugin/Core/Entity/CustomBlock.php index deabda4..f799e75 100644 --- a/core/modules/block/custom_block/lib/Drupal/custom_block/Plugin/Core/Entity/CustomBlock.php +++ b/core/modules/block/custom_block/lib/Drupal/custom_block/Plugin/Core/Entity/CustomBlock.php @@ -180,4 +180,22 @@ public function uri() { ) ); } + + /** + * {@inheritdoc} + */ + public function getInstances() { + return entity_load_multiple_by_properties('block', array('plugin' => 'custom_block:' . $this->uuid->value)); + } + + /** + * {@inheritdoc} + */ + public function delete() { + foreach ($this->getInstances() as $instance) { + $instance->delete(); + } + parent::delete(); + } + } 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..e44aa75 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,64 @@ 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); + + // Test getInstances method. + $this->assertEqual(1, count($block->getInstances())); + + // 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'); + + // Create another block with no instances, and test we don't get a + // confirmation message about deleting instances. + $edit3 = array(); + $edit3['info'] = $this->randomName(8); + $body = $this->randomName(16); + $edit3["block_body[$langcode][0][value]"] = $body; + $this->drupalPost('block/add/basic', $edit3, t('Save')); + + // Show the delete confirm form. + $this->drupalGet('block/3/delete'); + $this->assertNoText('This will also remove'); + } + }