From b59dc3b68c821a13eecbf4f797a3df926fd90b80 Mon Sep 17 00:00:00 2001 From: Andy Heathershaw Date: Tue, 12 Apr 2022 12:15:53 +0100 Subject: [PATCH] #3178 Allow a "thumbnails" property to be configured against a content type to control the output of the thumbnail link on the field when "getValue" is called on it --- config/bolt/contenttypes.yaml | 17 +++++++++++++++++ src/Entity/Field/ImageField.php | 15 +++++++++++++-- 2 files changed, 30 insertions(+), 2 deletions(-) diff --git a/config/bolt/contenttypes.yaml b/config/bolt/contenttypes.yaml index a74bc1bf3..df33d7f25 100644 --- a/config/bolt/contenttypes.yaml +++ b/config/bolt/contenttypes.yaml @@ -592,3 +592,20 @@ validationdemos: # filter: '*_xyz.twig' # taxonomy: [ tags ] # records_per_page: 20 +# +# Image field thumbnails +# +# To customise the thumbnail URL generated for a field from the getValue() function (or field.value) in Twig, +# add a "thumbnails" node to your field definition. +# +#contenttypexyz: +# name: ContentType Xyz +# slug: contenttypexyz +# fields: +# image: +# type: image +# thumbnails: +# size: [120,120] +# cropping: crop +# taxonomy: [ tags ] +# records_per_page: 20 diff --git a/src/Entity/Field/ImageField.php b/src/Entity/Field/ImageField.php index 3635f487c..9f0f8d3d9 100644 --- a/src/Entity/Field/ImageField.php +++ b/src/Entity/Field/ImageField.php @@ -64,7 +64,18 @@ public function getValue(): array $thumbPackage = new PathPackage('/thumbs/', new EmptyVersionStrategy()); $thumbnailHelper = new ThumbnailHelper(); - $path = $thumbnailHelper->path($this->get('filename'), 400, 400); + $fieldDefinition = $this->getDefinition(); + $path = isset($fieldDefinition['thumbnails']) + ? $thumbnailHelper->path( + $this->get('filename'), + isset($fieldDefinition['thumbnails']['size']) ? $fieldDefinition['thumbnails']['size'][0] : 400, + isset($fieldDefinition['thumbnails']['size']) ? $fieldDefinition['thumbnails']['size'][1] : 400, + null, + null, + isset($fieldDefinition['thumbnails']['cropping']) ? $fieldDefinition['thumbnails']['cropping'] : null + ) + : $thumbnailHelper->path($this->get('filename'), 400, 400); + $value['thumbnail'] = $thumbPackage->getUrl($path); return $value; @@ -88,7 +99,7 @@ public function setLinkedMedia(MediaRepository $mediaRepository): void if (! $this->get('filename')) { return; } - + $media = $mediaRepository->findOneByFullFilename($this->get('filename')); if ($media) {