diff --git a/core/modules/layout_builder/layout_builder.module b/core/modules/layout_builder/layout_builder.module index 74367cd728..a16b39a3f8 100644 --- a/core/modules/layout_builder/layout_builder.module +++ b/core/modules/layout_builder/layout_builder.module @@ -75,21 +75,21 @@ function layout_builder_form_entity_view_display_edit_form_alter(&$form, FormSta * @see layout_builder_form_entity_view_display_edit_form_alter() */ function layout_builder_form_entity_view_display_edit_entity_builder($entity_type_id, EntityViewDisplayInterface $display, &$form, FormStateInterface &$form_state) { - // Only proceed once the form has been submitted. - if (!$form_state->isSubmitted()) { - return; - } + $new_value = (bool) $form_state->getValue(['layout', 'allow_custom'], FALSE); + $display->setThirdPartySetting('layout_builder', 'allow_custom', $new_value); +} - $original_value = (bool) $display->getThirdPartySetting('layout_builder', 'allow_custom', FALSE); - $allow_custom = (bool) $form_state->getValue(['layout', 'allow_custom'], FALSE); - - // Only continue if the value has changed. - if ($original_value !== $allow_custom) { - $display->setThirdPartySetting('layout_builder', 'allow_custom', $allow_custom); +/** + * Implements hook_ENTITY_TYPE_presave(). + */ +function layout_builder_entity_view_display_presave(EntityViewDisplayInterface $display) { + $original_value = isset($display->original) ? $display->original->getThirdPartySetting('layout_builder', 'allow_custom', FALSE) : FALSE; + $new_value = $display->getThirdPartySetting('layout_builder', 'allow_custom', FALSE); + if ($original_value != $new_value) { $entity_type_id = $display->getTargetEntityTypeId(); $bundle = $display->getTargetBundle(); - if ($allow_custom) { + if ($new_value) { layout_builder_add_layout_section_field($entity_type_id, $bundle); } elseif ($field = FieldConfig::loadByName($entity_type_id, $bundle, 'layout_builder__layout')) { diff --git a/core/modules/layout_builder/tests/src/FunctionalJavascript/LayoutBuilderTest.php b/core/modules/layout_builder/tests/src/FunctionalJavascript/LayoutBuilderTest.php index 13ad29e300..9f4959f71b 100644 --- a/core/modules/layout_builder/tests/src/FunctionalJavascript/LayoutBuilderTest.php +++ b/core/modules/layout_builder/tests/src/FunctionalJavascript/LayoutBuilderTest.php @@ -165,9 +165,11 @@ public function testLayoutBuilderUi() { $this->drupalGet($this->node->toUrl('layout-builder')); $this->markCurrentPage(); + $assert_session->linkExists('Add Section'); $this->clickLink('Add Section'); $assert_session->assertWaitOnAjaxRequest(); + $assert_session->linkExists('Two column'); $this->clickLink('Two column'); $assert_session->assertWaitOnAjaxRequest(); @@ -187,6 +189,7 @@ public function testLayoutBuilderUi() { $assert_session->elementTextContains('css', '.layout__region--second', 'Powered by Drupal'); // Ensure the drag persisted after save. + $assert_session->linkExists('Save Layout'); $this->clickLink('Save Layout'); $assert_session->elementExists('css', '.layout__region--second .block-system-powered-by-block'); $assert_session->elementTextContains('css', '.layout__region--second', 'Powered by Drupal'); @@ -223,6 +226,7 @@ public function testLayoutBuilderUi() { $assert_session->addressEquals($this->node->toUrl('layout-builder')); $this->assertPageNotReloaded(); + $assert_session->linkExists('Save Layout'); $this->clickLink('Save Layout'); $assert_session->elementExists('css', '.layout'); @@ -230,9 +234,11 @@ public function testLayoutBuilderUi() { $this->drupalGet($this->node->toUrl('layout-builder')); $this->markCurrentPage(); + $assert_session->linkExists('Add Block'); $this->clickLink('Add Block'); $assert_session->assertWaitOnAjaxRequest(); + $assert_session->linkExists('My custom block'); $this->clickLink('My custom block'); $assert_session->assertWaitOnAjaxRequest(); @@ -241,12 +247,14 @@ public function testLayoutBuilderUi() { $assert_session->pageTextContains('This is the block content'); // Remove both sections. + $assert_session->linkExists('Remove section'); $this->clickLink('Remove section'); $assert_session->assertWaitOnAjaxRequest(); $page->pressButton('Remove'); $assert_session->assertWaitOnAjaxRequest(); + $assert_session->linkExists('Remove section'); $this->clickLink('Remove section'); $assert_session->assertWaitOnAjaxRequest(); @@ -257,6 +265,7 @@ public function testLayoutBuilderUi() { $assert_session->linkNotExists('Add Block'); $this->assertPageNotReloaded(); + $assert_session->linkExists('Save Layout'); $this->clickLink('Save Layout'); $assert_session->elementNotExists('css', '.layout'); } @@ -265,24 +274,32 @@ public function testLayoutBuilderUi() { * Tests configurable layouts. */ public function testConfigurableLayouts() { + entity_get_display('node', 'bundle_with_section_field', 'full') + ->setThirdPartySetting('layout_builder', 'allow_custom', TRUE) + ->save(); + $assert_session = $this->assertSession(); $page = $this->getSession()->getPage(); $this->drupalGet($this->node->toUrl('layout-builder')); $this->markCurrentPage(); + $assert_session->linkExists('Add Section'); $this->clickLink('Add Section'); $assert_session->assertWaitOnAjaxRequest(); $assert_session->elementExists('css', '#drupal-off-canvas'); + $assert_session->linkExists('One column'); $this->clickLink('One column'); $assert_session->assertWaitOnAjaxRequest(); // Add another section. + $assert_session->linkExists('Add Section'); $this->clickLink('Add Section'); $assert_session->assertWaitOnAjaxRequest(); $assert_session->elementExists('css', '#drupal-off-canvas'); + $assert_session->linkExists('Layout plugin (with settings)'); $this->clickLink('Layout plugin (with settings)'); $assert_session->assertWaitOnAjaxRequest(); $assert_session->fieldExists('layout_settings[setting_1]'); @@ -294,6 +311,7 @@ public function testConfigurableLayouts() { $assert_session->linkExists('Add Block'); // Configure the existing section. + $assert_session->linkExists('Configure section'); $this->clickLink('Configure section'); $assert_session->assertWaitOnAjaxRequest(); $page->fillField('layout_settings[setting_1]', 'Test setting value'); @@ -308,13 +326,19 @@ public function testConfigurableLayouts() { * Tests bypassing the Off Canvas dialog. */ public function testLayoutNoDialog() { + entity_get_display('node', 'bundle_with_section_field', 'full') + ->setThirdPartySetting('layout_builder', 'allow_custom', TRUE) + ->save(); + $assert_session = $this->assertSession(); $page = $this->getSession()->getPage(); // Set up a layout with one section. $this->drupalGet($this->node->toUrl('layout-builder')); + $assert_session->linkExists('Add Section'); $this->clickLink('Add Section'); $assert_session->assertWaitOnAjaxRequest(); + $assert_session->linkExists('One column'); $this->clickLink('One column'); $assert_session->assertWaitOnAjaxRequest();