From 4c899033a94532fdb421ada7b102ad4c760a45a7 Mon Sep 17 00:00:00 2001 From: Dave Long Date: Fri, 22 Nov 2024 13:08:20 +0000 Subject: [PATCH] Issue #3488664 by phenaproxima: The PlaceBlock config action breaks when placing a block in an empty region (cherry picked from commit 43717fbb0177d7107c003b89d683679a9c4bb7c5) --- .../src/Plugin/ConfigAction/PlaceBlock.php | 18 +++++++------- .../tests/src/Kernel/ConfigActionsTest.php | 24 +++++++++++++++++++ 2 files changed, 34 insertions(+), 8 deletions(-) diff --git a/core/modules/block/src/Plugin/ConfigAction/PlaceBlock.php b/core/modules/block/src/Plugin/ConfigAction/PlaceBlock.php index 19f51ca0827..820d36b8b9a 100644 --- a/core/modules/block/src/Plugin/ConfigAction/PlaceBlock.php +++ b/core/modules/block/src/Plugin/ConfigAction/PlaceBlock.php @@ -74,15 +74,17 @@ final class PlaceBlock implements ConfigActionPluginInterface, ContainerFactoryP 'theme' => $theme, 'region' => $value['region'], ]); - // Sort the blocks by weight. Don't use \Drupal\block\Entity\Block::sort() - // here because it seems to be intended to sort blocks in the UI, where - // we really just want to get the weights right in this situation. - uasort($blocks, fn (BlockInterface $a, BlockInterface $b) => $a->getWeight() <=> $b->getWeight()); + if ($blocks) { + // Sort the blocks by weight. Don't use \Drupal\block\Entity\Block::sort() + // here because it seems to be intended to sort blocks in the UI, where + // we really just want to get the weights right in this situation. + uasort($blocks, fn (BlockInterface $a, BlockInterface $b) => $a->getWeight() <=> $b->getWeight()); - $value['weight'] = match ($value['position']) { - 'first' => reset($blocks)->getWeight() - 1, - 'last' => end($blocks)->getWeight() + 1, - }; + $value['weight'] = match ($value['position']) { + 'first' => reset($blocks)->getWeight() - 1, + 'last' => end($blocks)->getWeight() + 1, + }; + } } // Remove values that are not valid properties of block entities. unset($value['position'], $value['default_region']); diff --git a/core/modules/block/tests/src/Kernel/ConfigActionsTest.php b/core/modules/block/tests/src/Kernel/ConfigActionsTest.php index 70392f0e790..1157ad9ec0e 100644 --- a/core/modules/block/tests/src/Kernel/ConfigActionsTest.php +++ b/core/modules/block/tests/src/Kernel/ConfigActionsTest.php @@ -163,4 +163,28 @@ class ConfigActionsTest extends KernelTestBase { $this->assertSame('last', end($blocks)); } + /** + * Tests using the PlaceBlock action in an empty region. + */ + public function testPlaceBlockInEmptyRegion(): void { + /** @var \Drupal\Core\Entity\Query\QueryInterface $query */ + $query = $this->container->get(EntityTypeManagerInterface::class) + ->getStorage('block') + ->getQuery() + ->count() + ->condition('theme', 'olivero') + ->condition('region', 'footer_top'); + $this->assertSame(0, $query->execute()); + + // Place a block in that region. + $this->configActionManager->applyAction('placeBlockInDefaultTheme', 'block.block.test', [ + 'plugin' => 'system_powered_by_block', + 'region' => [ + 'olivero' => 'footer_top', + ], + 'position' => 'first', + ]); + $this->assertSame(1, $query->execute()); + } + }