Commit a74a17a5 authored by alexpott's avatar alexpott

Issue #1994146 by larowlan, kim.pepper: Fixed WSOD after deleting a custom block content entity.

parent 7b0039e9
......@@ -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();
......
......@@ -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();
}
......@@ -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();
}
}
......@@ -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');
}
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment