Issue #3325167 by smustgrave, acbramley, murilohp, rkoller, larowlan, AaronMcHale, dww, BramDriesen: Revisit the redirect to 'add block' form in the 'add block content' form

merge-requests/5741/head
Lauri Eskola 2023-12-08 11:30:24 +02:00
parent 7bd749f16a
commit fca86410e6
No known key found for this signature in database
GPG Key ID: 382FC0F5B0DF53F8
3 changed files with 101 additions and 17 deletions

View File

@ -38,6 +38,51 @@ class BlockContentForm extends ContentEntityForm {
return $form;
}
/**
* {@inheritdoc}
*/
public function actions(array $form, FormStateInterface $form_state): array {
$element = parent::actions($form, $form_state);
if ($this->getRequest()->query->has('theme')) {
$element['submit']['#value'] = $this->t('Save and configure');
}
if ($this->currentUser()->hasPermission('administer blocks') && !$this->getRequest()->query->has('theme') && $this->entity->isNew()) {
$element['configure_block'] = [
'#type' => 'submit',
'#value' => $this->t('Save and configure'),
'#weight' => 20,
'#submit' => array_merge($element['submit']['#submit'], ['::configureBlock']),
];
}
return $element;
}
/**
* Form submission handler for the 'configureBlock' action.
*
* @param array $form
* An associative array containing the structure of the form.
* @param \Drupal\Core\Form\FormStateInterface $form_state
* The current state of the form.
*/
public function configureBlock(array $form, FormStateInterface $form_state): void {
$block = $this->entity;
if (!$theme = $block->getTheme()) {
$theme = $this->config('system.theme')->get('default');
}
$form_state->setRedirect(
'block.admin_add',
[
'plugin_id' => 'block_content:' . $block->uuid(),
'theme' => $theme,
]
);
$form_state->setIgnoreDestination();
}
/**
* {@inheritdoc}
*/
@ -64,19 +109,19 @@ class BlockContentForm extends ContentEntityForm {
$form_state->setValue('id', $block->id());
$form_state->set('id', $block->id());
if ($insert) {
if (!$theme = $block->getTheme()) {
$theme = $this->config('system.theme')->get('default');
$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'));
}
$form_state->setRedirect(
'block.admin_add',
[
'plugin_id' => 'block_content:' . $block->uuid(),
'theme' => $theme,
]
);
}
else {
$form_state->setRedirectUrl($block->toUrl('collection'));
}
}
else {

View File

@ -95,10 +95,7 @@ class BlockContentCreationTest extends BlockContentTestBase {
$edit['info[0][value]'] = 'Test Block';
$edit['body[0][value]'] = $this->randomMachineName(16);
$this->drupalGet('block/add/basic');
$this->submitForm($edit, 'Save');
// Check that the Basic block has been created.
$this->assertSession()->pageTextContains('basic ' . $edit['info[0][value]'] . ' has been created.');
$this->submitForm($edit, 'Save and configure');
// Save our block permanently
$this->submitForm(['region' => 'content'], 'Save block');
@ -143,6 +140,48 @@ class BlockContentCreationTest extends BlockContentTestBase {
$this->assertNotEmpty($block, 'Content Block found in database.');
}
/**
* Tests the redirect workflow of creating a block_content and block.
*/
public function testBlockContentFormSubmitHandlers() {
$this->drupalLogin($this->adminUser);
// Create a block and place in block layout.
$this->drupalGet('/admin/content/block');
$this->clickLink('Add content block');
// Verify destination URL, when clicking "Save and configure" this
// destination will be ignored.
$base = base_path();
$url = 'block/add?destination=' . $base . 'admin/content/block';
$this->assertSession()->addressEquals($url);
$edit = [];
$edit['info[0][value]'] = 'Test Block';
$edit['body[0][value]'] = $this->randomMachineName(16);
$this->submitForm($edit, 'Save and configure');
$this->assertSession()->pageTextContains('basic ' . $edit['info[0][value]'] . ' has been created.');
$this->assertSession()->pageTextContains('Configure block');
// Verify when editing a block "Save and configure" does not appear.
$this->drupalGet('/admin/content/block/1');
$this->assertSession()->buttonNotExists('Save and configure');
// Create a block but go back to block library.
$edit = [];
$edit['info[0][value]'] = 'Test Block';
$edit['body[0][value]'] = $this->randomMachineName(16);
$this->drupalGet('block/add/basic');
$this->submitForm($edit, 'Save');
// Check that the Basic block has been created.
$this->assertSession()->pageTextContains('basic ' . $edit['info[0][value]'] . ' has been created.');
$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');
$this->assertSession()->buttonNotExists('Save and configure');
}
/**
* Create a default content block.
*

View File

@ -233,7 +233,7 @@ class BlockContentTypeTest extends BlockContentTestBase {
$this->clickLink('foo');
// Create a new block.
$edit = ['info[0][value]' => $this->randomMachineName(8)];
$this->submitForm($edit, 'Save');
$this->submitForm($edit, 'Save and configure');
$blocks = $storage->loadByProperties(['info' => $edit['info[0][value]']]);
if (!empty($blocks)) {
$block = reset($blocks);