Verified Commit 5acde36f authored by Théodore Biadala's avatar Théodore Biadala
Browse files

Issue #3462896 by phenaproxima, mandclu: Allow 'region' key to be a string for...

Issue #3462896 by phenaproxima, mandclu: Allow 'region' key to be a string for placeBlockInDefaultTheme and placeBlockInAdminTheme config actions
parent d075b6c3
Loading
Loading
Loading
Loading
Loading
+1 −2
Original line number Diff line number Diff line
@@ -57,13 +57,12 @@ public function apply(string $configName, mixed $value): void {
    $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.");
    }

+19 −2
Original line number Diff line number Diff line
@@ -95,7 +95,7 @@ public function testPlaceBlockActionDoesNotChangeExistingBlock(): void {
   * @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 @@ public function testPlaceBlockInTheme(string $action, string $expected_theme, st
    ]);
  }

  /**
   * @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 {