Skip to content

Commit

Permalink
Merge pull request #3179 from andysh-uk/issue-3178-image-field-thumbn…
Browse files Browse the repository at this point in the history
…ails

#3178 Allow a "thumbnails" property to be configured against a content type field
  • Loading branch information
bobdenotter authored Apr 14, 2022
2 parents f21d2bb + b59dc3b commit ef95ad3
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 2 deletions.
17 changes: 17 additions & 0 deletions config/bolt/contenttypes.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -615,3 +615,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
15 changes: 13 additions & 2 deletions src/Entity/Field/ImageField.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -88,7 +99,7 @@ public function setLinkedMedia(MediaRepository $mediaRepository): void
if (! $this->get('filename')) {
return;
}

$media = $mediaRepository->findOneByFullFilename($this->get('filename'));

if ($media) {
Expand Down

0 comments on commit ef95ad3

Please sign in to comment.