Skip to content

Allow a "thumbnails" property to be configured against a ContentType Field #3179

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions config/bolt/contenttypes.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
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