Issue #3412283 by mathilde_dumond, acbramley, Berdir, smustgrave, BramDriesen, larowlan: Editing a block_content entity no longer redirects to the overview

(cherry picked from commit b46fb5e627)
merge-requests/6880/merge
catch 2024-03-05 09:44:44 +00:00
parent 746540d1d8
commit 42a8ae2b66
2 changed files with 37 additions and 26 deletions

View File

@ -108,20 +108,18 @@ class BlockContentForm extends ContentEntityForm {
if ($block->id()) {
$form_state->setValue('id', $block->id());
$form_state->set('id', $block->id());
if ($insert) {
$theme = $block->getTheme();
if ($theme) {
$form_state->setRedirect(
'block.admin_add',
[
'plugin_id' => 'block_content:' . $block->uuid(),
'theme' => $theme,
]
);
}
else {
$form_state->setRedirectUrl($block->toUrl('collection'));
}
$theme = $block->getTheme();
if ($insert && $theme) {
$form_state->setRedirect(
'block.admin_add',
[
'plugin_id' => 'block_content:' . $block->uuid(),
'theme' => $theme,
]
);
}
else {
$form_state->setRedirectUrl($block->toUrl('collection'));
}
}
else {

View File

@ -4,6 +4,7 @@ declare(strict_types=1);
namespace Drupal\Tests\block_content\Functional;
use Drupal\block_content\BlockContentInterface;
use Drupal\block_content\Entity\BlockContent;
use Drupal\Core\Database\Database;
@ -69,10 +70,7 @@ class BlockContentCreationTest extends BlockContentTestBase {
$this->assertSession()->fieldNotExists('settings[view_mode]');
// Check that the block exists in the database.
$blocks = \Drupal::entityTypeManager()
->getStorage('block_content')
->loadByProperties(['info' => $edit['info[0][value]']]);
$block = reset($blocks);
$block = $this->getBlockByLabel($edit['info[0][value]']);
$this->assertNotEmpty($block, 'Content Block found in database.');
}
@ -135,10 +133,7 @@ class BlockContentCreationTest extends BlockContentTestBase {
$this->assertSession()->fieldValueEquals('settings[view_mode]', 'test_view_mode');
// Check that the block exists in the database.
$blocks = \Drupal::entityTypeManager()
->getStorage('block_content')
->loadByProperties(['info' => $edit['info[0][value]']]);
$block = reset($blocks);
$block = $this->getBlockByLabel($edit['info[0][value]']);
$this->assertNotEmpty($block, 'Content Block found in database.');
}
@ -177,6 +172,14 @@ class BlockContentCreationTest extends BlockContentTestBase {
$this->assertSession()->pageTextContains('basic ' . $edit['info[0][value]'] . ' has been created.');
$this->assertSession()->addressEquals('/admin/content/block');
// Check that the user is redirected to the block library on edit.
$block = $this->getBlockByLabel($edit['info[0][value]']);
$this->drupalGet($block->toUrl('edit-form'));
$this->submitForm([
'info[0][value]' => 'Test Block Updated',
], 'Save');
$this->assertSession()->addressEquals('admin/content/block');
// Test with user who doesn't have permission to place a block.
$this->drupalLogin($this->drupalCreateUser(['administer block content']));
$this->drupalGet('block/add/basic');
@ -202,10 +205,7 @@ class BlockContentCreationTest extends BlockContentTestBase {
$this->assertSession()->pageTextContains('basic ' . $edit['info[0][value]'] . ' has been created.');
// Check that the block exists in the database.
$blocks = \Drupal::entityTypeManager()
->getStorage('block_content')
->loadByProperties(['info' => $edit['info[0][value]']]);
$block = reset($blocks);
$block = $this->getBlockByLabel($edit['info[0][value]']);
$this->assertNotEmpty($block, 'Default Content Block found in database.');
}
@ -318,4 +318,17 @@ class BlockContentCreationTest extends BlockContentTestBase {
$this->assertEquals($block_placement_id, $block_placement->id(), "The block placement config entity has a dependency on the block content entity.");
}
/**
* Load a block based on the label.
*/
private function getBlockByLabel(string $label): ?BlockContentInterface {
$blocks = \Drupal::entityTypeManager()
->getStorage('block_content')
->loadByProperties(['info' => $label]);
if (empty($blocks)) {
return NULL;
}
return reset($blocks);
}
}