diff --git a/core/modules/layout_builder/src/Form/MoveBlockForm.php b/core/modules/layout_builder/src/Form/MoveBlockForm.php index d051de47e8392c11a99010f97c2a319bb5bf4c06..beb3aa9757250d74e7b1bc256320dbe221b7a0f4 100644 --- a/core/modules/layout_builder/src/Form/MoveBlockForm.php +++ b/core/modules/layout_builder/src/Form/MoveBlockForm.php @@ -190,6 +190,7 @@ public function buildForm(array $form, FormStateInterface $form_state, SectionSt if (!isset($components[$uuid])) { $components[$uuid] = $sections[$delta]->getComponent($uuid); } + $state_weight_delta = round(count($components) / 2); foreach ($components as $component_uuid => $component) { /** @var \Drupal\Core\Block\BlockPluginInterface $plugin */ $plugin = $component->getPlugin(); @@ -222,6 +223,7 @@ public function buildForm(array $form, FormStateInterface $form_state, SectionSt '#attributes' => [ 'class' => ['table-sort-weight'], ], + '#delta' => $state_weight_delta, ], ]; } diff --git a/core/modules/layout_builder/tests/src/FunctionalJavascript/MoveBlockFormTest.php b/core/modules/layout_builder/tests/src/FunctionalJavascript/MoveBlockFormTest.php index d6b0093271e56a3be23a525d5d65b687a07ae763..4f739703066c1662fe86a6d8858029e48f58d1c6 100644 --- a/core/modules/layout_builder/tests/src/FunctionalJavascript/MoveBlockFormTest.php +++ b/core/modules/layout_builder/tests/src/FunctionalJavascript/MoveBlockFormTest.php @@ -96,6 +96,7 @@ protected function setUp(): void { */ public function testMoveBlock() { $page = $this->getSession()->getPage(); + $assert_session = $this->assertSession(); // Reorder body field in current region. $this->openBodyMoveForm(1, 'content', ['Links', 'Body (current)']); @@ -135,6 +136,47 @@ public function testMoveBlock() { $this->assertBlockTable(['Body (current)']); $page->pressButton('Move'); $this->assertRegionBlocksOrder(0, 'second', ['.block-field-blocknodebundle-with-section-fieldbody']); + + // The weight element uses -10 to 10 by default, which can cause bugs. + // Add 25 'Powered by Drupal' blocks to a new section. + $page->clickLink('Add section'); + $assert_session->waitForElementVisible('css', '#drupal-off-canvas'); + $assert_session->assertWaitOnAjaxRequest(); + $page->clickLink('One column'); + $assert_session->assertWaitOnAjaxRequest(); + $this->assertNotEmpty($assert_session->waitForElementVisible('css', 'input[value="Add section"]')); + $page->pressButton('Add section'); + $assert_session->assertNoElementAfterWait('css', '#drupal-off-canvas'); + $large_block_number = 25; + for ($i = 0; $i < $large_block_number; $i++) { + $assert_session->elementExists('css', '[data-layout-delta="0"].layout--onecol [data-region="content"] .layout-builder__add-block')->click(); + $this->assertNotEmpty($assert_session->waitForElementVisible('css', '#drupal-off-canvas a:contains("Powered by Drupal")')); + $assert_session->assertWaitOnAjaxRequest(); + $page->clickLink('Powered by Drupal'); + $this->assertNotEmpty($assert_session->waitForElementVisible('css', 'input[value="Add block"]')); + $assert_session->assertWaitOnAjaxRequest(); + $page->pressButton('Add block'); + $assert_session->assertNoElementAfterWait('css', '#drupal-off-canvas'); + } + $first_region_block_locator = '[data-layout-delta="0"].layout--onecol [data-region="content"] [data-layout-block-uuid]'; + $assert_session->elementsCount('css', $first_region_block_locator, $large_block_number); + + // Move the Body block to the end of the new section. + $this->openBodyMoveForm(1, 'second', ['Body (current)']); + $page->selectFieldOption('Region', '0:content'); + $expected_block_table = array_fill(0, $large_block_number, 'Powered by Drupal'); + $expected_block_table[] = 'Body (current)'; + $this->assertBlockTable($expected_block_table); + $expected_block_table = array_fill(0, $large_block_number - 1, 'Powered by Drupal'); + $expected_block_table[] = 'Body (current)*'; + $expected_block_table[] = 'Powered by Drupal'; + $this->moveBlockWithKeyboard('up', 'Body', $expected_block_table); + $page->pressButton('Move'); + $assert_session->assertNoElementAfterWait('css', '#drupal-off-canvas'); + // Get all blocks currently in the region. + $blocks = $page->findAll('css', $first_region_block_locator); + // The second to last $block should be the body. + $this->assertTrue($blocks[count($blocks) - 2]->hasClass('block-field-blocknodebundle-with-section-fieldbody')); } /**