From 69243b646501652ba33253c90e806220ea3572db Mon Sep 17 00:00:00 2001 From: "M. Vondano" Date: Wed, 3 Mar 2021 18:12:32 +0100 Subject: [PATCH] Correctly merge image size _defaults (see #2783) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Description ----------- | Q | A | -----------------| --- | Fixed issues | - | Docs PR or issue | - Currently defining `formats` under the image size `_defaults` is broken. This is because the processed config values of the individual size definitions already contain an empty `formats` array which then wins in `array_merge`. This PR fixes this problem by giving the values under `_defaults` precedence over empty arrays. Unfortunately this case was missing from the tests and went unnoticed (added now)… Commits ------- b7b7b601 merge _defaults with precedence over empty arrays 990be331 add reference af36ec25 Merge branch '4.11' into bugfix/image-size-defaults-empty-arrays --- .../DependencyInjection/ContaoCoreExtension.php | 6 +++++- .../ContaoCoreExtensionTest.php | 15 ++++++++++++--- 2 files changed, 17 insertions(+), 4 deletions(-) diff --git a/core-bundle/src/DependencyInjection/ContaoCoreExtension.php b/core-bundle/src/DependencyInjection/ContaoCoreExtension.php index 905cd4435c2..f9662ec447c 100644 --- a/core-bundle/src/DependencyInjection/ContaoCoreExtension.php +++ b/core-bundle/src/DependencyInjection/ContaoCoreExtension.php @@ -185,7 +185,11 @@ private function setPredefinedImageSizes(array $config, ContainerBuilder $contai } if (isset($config['image']['sizes']['_defaults'])) { - $value = array_merge($config['image']['sizes']['_defaults'], $value); + // Make sure that arrays defined under _defaults will take precedence over empty arrays (see #2783) + $value = array_merge( + $config['image']['sizes']['_defaults'], + array_filter($value, static function ($v) { return !\is_array($v) || !empty($v); }) + ); } $imageSizes['_'.$name] = $this->camelizeKeys($value); diff --git a/core-bundle/tests/DependencyInjection/ContaoCoreExtensionTest.php b/core-bundle/tests/DependencyInjection/ContaoCoreExtensionTest.php index cf884e4f41a..2ec1c7d2b84 100644 --- a/core-bundle/tests/DependencyInjection/ContaoCoreExtensionTest.php +++ b/core-bundle/tests/DependencyInjection/ContaoCoreExtensionTest.php @@ -3980,6 +3980,9 @@ public function testRegistersThePredefinedImageSizes(): void 'sizes' => [ '_defaults' => [ 'width' => 150, + 'formats' => [ + 'jpg' => ['webp', 'jpg'], + ], ], 'foo' => [ 'height' => 250, @@ -4026,12 +4029,16 @@ public function testRegistersThePredefinedImageSizes(): void 'width' => 150, 'height' => 250, 'items' => [], - 'formats' => [], + 'formats' => [ + 'jpg' => ['webp', 'jpg'], + ], ], '_bar' => [ 'width' => 150, 'items' => [], - 'formats' => [], + 'formats' => [ + 'jpg' => ['webp', 'jpg'], + ], ], '_foobar' => [ 'width' => 100, @@ -4052,7 +4059,9 @@ public function testRegistersThePredefinedImageSizes(): void 'sizes' => '50vw', 'media' => '(max-width: 900px)', ]], - 'formats' => [], + 'formats' => [ + 'jpg' => ['webp', 'jpg'], + ], ], ]];