Skip to content

Commit

Permalink
Correctly merge image size _defaults (see #2783)
Browse files Browse the repository at this point in the history
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
-------

b7b7b60 merge _defaults with precedence over empty arrays
990be33 add reference
af36ec2 Merge branch '4.11' into bugfix/image-size-defaults-empty-arrays
  • Loading branch information
m-vo authored and fritzmg committed Mar 4, 2021
1 parent 7fc3159 commit 69243b6
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 4 deletions.
6 changes: 5 additions & 1 deletion core-bundle/src/DependencyInjection/ContaoCoreExtension.php
Expand Up @@ -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);
Expand Down
15 changes: 12 additions & 3 deletions core-bundle/tests/DependencyInjection/ContaoCoreExtensionTest.php
Expand Up @@ -3980,6 +3980,9 @@ public function testRegistersThePredefinedImageSizes(): void
'sizes' => [
'_defaults' => [
'width' => 150,
'formats' => [
'jpg' => ['webp', 'jpg'],
],
],
'foo' => [
'height' => 250,
Expand Down Expand Up @@ -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,
Expand All @@ -4052,7 +4059,9 @@ public function testRegistersThePredefinedImageSizes(): void
'sizes' => '50vw',
'media' => '(max-width: 900px)',
]],
'formats' => [],
'formats' => [
'jpg' => ['webp', 'jpg'],
],
],
]];

Expand Down

0 comments on commit 69243b6

Please sign in to comment.