From 385c60a30b8b188f93240f01b1edae2b5371ca59 Mon Sep 17 00:00:00 2001 From: Benni Mack <benni@typo3.org> Date: Thu, 21 Sep 2023 15:59:37 +0200 Subject: [PATCH] [BUGFIX] Avoid PHP 8 warning when converting crop in Image ViewHelpers MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Resolves: #101185 Releases: main, 12.4, 11.5 Change-Id: If54f9308ac2928ce9521228593d6a3ef88713d51 Reviewed-on: https://review.typo3.org/c/Packages/TYPO3.CMS/+/81157 Reviewed-by: Oliver Klee <typo3-coding@oliverklee.de> Reviewed-by: Stefan Bürk <stefan@buerk.tech> Tested-by: Oliver Klee <typo3-coding@oliverklee.de> Tested-by: Stefan Bürk <stefan@buerk.tech> Tested-by: core-ci <typo3@b13.com> --- .../sysext/fluid/Classes/ViewHelpers/ImageViewHelper.php | 8 +++++++- .../fluid/Classes/ViewHelpers/Uri/ImageViewHelper.php | 7 ++++++- .../Tests/Functional/ViewHelpers/ImageViewHelperTest.php | 4 ++++ 3 files changed, 17 insertions(+), 2 deletions(-) diff --git a/typo3/sysext/fluid/Classes/ViewHelpers/ImageViewHelper.php b/typo3/sysext/fluid/Classes/ViewHelpers/ImageViewHelper.php index b88dad895907..0d871ceb1e2c 100644 --- a/typo3/sysext/fluid/Classes/ViewHelpers/ImageViewHelper.php +++ b/typo3/sysext/fluid/Classes/ViewHelpers/ImageViewHelper.php @@ -119,7 +119,7 @@ final class ImageViewHelper extends AbstractTagBasedViewHelper $this->registerArgument('src', 'string', 'a path to a file, a combined FAL identifier or an uid (int). If $treatIdAsReference is set, the integer is considered the uid of the sys_file_reference record. If you already got a FAL object, consider using the $image parameter instead', false, ''); $this->registerArgument('treatIdAsReference', 'bool', 'given src argument is a sys_file_reference record', false, false); $this->registerArgument('image', 'object', 'a FAL object (\\TYPO3\\CMS\\Core\\Resource\\File or \\TYPO3\\CMS\\Core\\Resource\\FileReference)'); - $this->registerArgument('crop', 'string|bool', 'overrule cropping of image (setting to FALSE disables the cropping set in FileReference)'); + $this->registerArgument('crop', 'string|bool|array', 'overrule cropping of image (setting to FALSE disables the cropping set in FileReference)'); $this->registerArgument('cropVariant', 'string', 'select a cropping variant, in case multiple croppings have been specified or stored in FileReference', false, 'default'); $this->registerArgument('fileExtension', 'string', 'Custom file extension to use'); @@ -159,6 +159,12 @@ final class ImageViewHelper extends AbstractTagBasedViewHelper if ($cropString === null && $image->hasProperty('crop') && $image->getProperty('crop')) { $cropString = $image->getProperty('crop'); } + + // CropVariantCollection needs a string, but this VH could also receive an array + if (is_array($cropString)) { + $cropString = json_encode($cropString); + } + $cropVariantCollection = CropVariantCollection::create((string)$cropString); $cropVariant = $this->arguments['cropVariant'] ?: 'default'; $cropArea = $cropVariantCollection->getCropArea($cropVariant); diff --git a/typo3/sysext/fluid/Classes/ViewHelpers/Uri/ImageViewHelper.php b/typo3/sysext/fluid/Classes/ViewHelpers/Uri/ImageViewHelper.php index 253f8dc684e2..215908e9dbd3 100644 --- a/typo3/sysext/fluid/Classes/ViewHelpers/Uri/ImageViewHelper.php +++ b/typo3/sysext/fluid/Classes/ViewHelpers/Uri/ImageViewHelper.php @@ -93,7 +93,7 @@ final class ImageViewHelper extends AbstractViewHelper $this->registerArgument('src', 'string', 'src', false, ''); $this->registerArgument('treatIdAsReference', 'bool', 'given src argument is a sys_file_reference record', false, false); $this->registerArgument('image', 'object', 'image'); - $this->registerArgument('crop', 'string|bool', 'overrule cropping of image (setting to FALSE disables the cropping set in FileReference)'); + $this->registerArgument('crop', 'string|bool|array', 'overrule cropping of image (setting to FALSE disables the cropping set in FileReference)'); $this->registerArgument('cropVariant', 'string', 'select a cropping variant, in case multiple croppings have been specified or stored in FileReference', false, 'default'); $this->registerArgument('fileExtension', 'string', 'Custom file extension to use'); @@ -135,6 +135,11 @@ final class ImageViewHelper extends AbstractViewHelper $cropString = $image->getProperty('crop'); } + // CropVariantCollection needs a string, but this VH could also receive an array + if (is_array($cropString)) { + $cropString = json_encode($cropString); + } + $cropVariantCollection = CropVariantCollection::create((string)$cropString); $cropVariant = $arguments['cropVariant'] ?: 'default'; $cropArea = $cropVariantCollection->getCropArea($cropVariant); diff --git a/typo3/sysext/fluid/Tests/Functional/ViewHelpers/ImageViewHelperTest.php b/typo3/sysext/fluid/Tests/Functional/ViewHelpers/ImageViewHelperTest.php index fe6da45f956f..f957d9c63fb6 100644 --- a/typo3/sysext/fluid/Tests/Functional/ViewHelpers/ImageViewHelperTest.php +++ b/typo3/sysext/fluid/Tests/Functional/ViewHelpers/ImageViewHelperTest.php @@ -61,6 +61,10 @@ final class ImageViewHelperTest extends FunctionalTestCase '<f:image src="EXT:fluid/Tests/Functional/Fixtures/ViewHelpers/ImageViewHelperTest.jpg" width="300" height="500" crop="null" />', '@^<img src="typo3temp/assets/_processed_/b/3/csm_ImageViewHelperTest_.*\.jpg" width="300" height="500" alt="" />$@', ], + 'crop as array' => [ + '<f:image src="EXT:fluid/Tests/Functional/Fixtures/ViewHelpers/ImageViewHelperTest.jpg" width="300" height="500" crop="{\'x\': 200, \'y\': 200, \'width\': 200, \'height\': 200}" />', + '@^<img src="typo3temp/assets/_processed_/b/3/csm_ImageViewHelperTest_.*\.jpg" width="300" height="500" alt="" />$@', + ], 'jpg file extension' => [ '<f:image src="EXT:fluid/Tests/Functional/Fixtures/ViewHelpers/ImageViewHelperTest.jpg" width="300" height="500" crop="null" fileExtension="jpg" />', '@^<img src="typo3temp/assets/_processed_/b/3/csm_ImageViewHelperTest_.*\.jpg" width="300" height="500" alt="" />$@', -- GitLab