From 5acde36facd4b09d690b9e79b04b9b1e5cc073c9 Mon Sep 17 00:00:00 2001 From: nod_ Date: Fri, 6 Sep 2024 15:13:24 +0200 Subject: [PATCH] Issue #3462896 by phenaproxima, mandclu: Allow 'region' key to be a string for placeBlockInDefaultTheme and placeBlockInAdminTheme config actions --- .../src/Plugin/ConfigAction/PlaceBlock.php | 3 +-- .../tests/src/Kernel/ConfigActionsTest.php | 21 +++++++++++++++++-- 2 files changed, 20 insertions(+), 4 deletions(-) diff --git a/core/modules/block/src/Plugin/ConfigAction/PlaceBlock.php b/core/modules/block/src/Plugin/ConfigAction/PlaceBlock.php index 19f51ca0827..512ad99facb 100644 --- a/core/modules/block/src/Plugin/ConfigAction/PlaceBlock.php +++ b/core/modules/block/src/Plugin/ConfigAction/PlaceBlock.php @@ -57,13 +57,12 @@ final class PlaceBlock implements ConfigActionPluginInterface, ContainerFactoryP $theme = $this->configFactory->get('system.theme')->get($this->whichTheme); $value['theme'] = $theme; - if (array_key_exists('region', $value)) { + if (array_key_exists('region', $value) && is_array($value['region'])) { // Since the recipe author might not know ahead of time what theme the // block is in, they should supply a map whose keys are theme names and // values are region names, so we know where to place this block. If the // target theme is not in the map, they should supply the name of a // fallback region. If all that fails, give up with an exception. - assert(is_array($value['region'])); $value['region'] = $value['region'][$theme] ?? $value['default_region'] ?? throw new ConfigActionException("Cannot determine which region to place this block into, because no default region was provided."); } diff --git a/core/modules/block/tests/src/Kernel/ConfigActionsTest.php b/core/modules/block/tests/src/Kernel/ConfigActionsTest.php index 5fa62b12082..37fbe780cc7 100644 --- a/core/modules/block/tests/src/Kernel/ConfigActionsTest.php +++ b/core/modules/block/tests/src/Kernel/ConfigActionsTest.php @@ -95,7 +95,7 @@ class ConfigActionsTest extends KernelTestBase { * @testWith ["placeBlockInDefaultTheme", "olivero", "header"] * ["placeBlockInAdminTheme", "claro", "page_bottom"] */ - public function testPlaceBlockInTheme(string $action, string $expected_theme, string $expected_region): void { + public function testPlaceBlockInDynamicRegion(string $action, string $expected_theme, string $expected_region): void { $this->configActionManager->applyAction($action, 'block.block.test_block', [ 'plugin' => 'system_powered_by_block', 'region' => [ @@ -119,9 +119,26 @@ class ConfigActionsTest extends KernelTestBase { ]); } + /** + * @testWith ["placeBlockInDefaultTheme", "olivero"] + * ["placeBlockInAdminTheme", "claro"] + */ + public function testPlaceBlockInStaticRegion(string $action, string $expected_theme): void { + $this->configActionManager->applyAction($action, 'block.block.test_block', [ + 'plugin' => 'system_powered_by_block', + 'region' => 'content', + ]); + + $block = Block::load('test_block'); + $this->assertInstanceOf(Block::class, $block); + $this->assertSame('system_powered_by_block', $block->getPluginId()); + $this->assertSame($expected_theme, $block->getTheme()); + $this->assertSame('content', $block->getRegion()); + } + public function testPlaceBlockInDefaultRegion(): void { $this->config('system.theme')->set('default', 'umami')->save(); - $this->testPlaceBlockInTheme('placeBlockInDefaultTheme', 'umami', 'content'); + $this->testPlaceBlockInDynamicRegion('placeBlockInDefaultTheme', 'umami', 'content'); } public function testPlaceBlockAtPosition(): void {