From 8e981a7d1f6369e6a4108f4a5856909a9bbd69ca Mon Sep 17 00:00:00 2001 From: John Flatness Date: Tue, 10 Dec 2024 17:35:43 -0500 Subject: [PATCH] Render non-browser-native images in gallery We'll include these in the gallery when we have thumbnails for them and they're of an image/* mimetype, but they're not in the whitelist as being browser-supported; these will get rendered by showing the "large" thumbnail image. Alongside this there's some cleanup/refactoring to the lightgallery helper, and a change to the gallery block for media to make it only use the gallery when the gallery supports the media being viewed, otherwise falling back to the standard embed. (fix #2068) --- .../src/View/Helper/LightGalleryOutput.php | 48 ++++++++++++++----- application/src/View/Helper/SortMedia.php | 6 ++- .../lightbox-gallery-media.phtml | 10 ++-- 3 files changed, 47 insertions(+), 17 deletions(-) diff --git a/application/src/View/Helper/LightGalleryOutput.php b/application/src/View/Helper/LightGalleryOutput.php index 4443d1cdcc..eb4fdc7413 100644 --- a/application/src/View/Helper/LightGalleryOutput.php +++ b/application/src/View/Helper/LightGalleryOutput.php @@ -25,15 +25,16 @@ public function __invoke($files = null) $mediaCaption = $view->themeSetting('media_caption'); foreach ($files as $file) { + $attribs = []; $media = $file['media']; - $source = ($media->originalUrl()) ? $media->originalUrl() : $media->source(); - $mediaCaptionOptions = [ - 'none' => '', - 'title' => 'data-sub-html="' . $media->displayTitle() . '"', - 'description' => 'data-sub-html="' . $media->displayDescription() . '"', - ]; - $mediaCaptionAttribute = ($mediaCaption) ? $mediaCaptionOptions[$mediaCaption] : ''; - $mediaType = $media->mediatype(); + if (!empty($file['forceThumbnail'])) { + $source = $media->thumbnailUrl('large'); + $downloadUrl = $media->originalUrl() ?: $source; + } else { + $source = $downloadUrl = $media->originalUrl() ?: $media->source(); + } + + $mediaType = $media->mediaType(); if (null !== $mediaType && strpos($mediaType, 'video') !== false) { $videoSrcObject = [ 'source' => [ @@ -60,14 +61,35 @@ public function __invoke($files = null) } } $videoSrcJson = json_encode($videoSrcObject); - $html .= '
'; + $attribs['data-video'] = $videoSrcJson; } elseif ($mediaType == 'application/pdf') { - $html .= '
'; + $attribs['data-iframe'] = $source; + $attribs['data-src'] = $source; } else { - $html .= '
'; + $attribs['data-src'] = $source; + } + + switch ($mediaCaption) { + case 'title': + $attribs['data-sub-html'] = $media->displayTitle(); + break; + case 'description': + $attribs['data-sub-html'] = $media->displayDescription(); + break; + case 'none': + default: + // no action + } + + $attribs['data-thumb'] = $media->thumbnailDisplayUrl('medium'); + $attribs['data-download-url'] = $downloadUrl; + $attribs['class'] = 'media resource'; + + $html .= ' $attribValue) { + $html .= ' ' . $attribName . '="' . $escape($attribValue) . '"'; } - $html .= $media->render(); - $html .= '
'; + $html .= '>
'; } $html .= '
'; diff --git a/application/src/View/Helper/SortMedia.php b/application/src/View/Helper/SortMedia.php index d62b0b3b3f..d22f6349c4 100644 --- a/application/src/View/Helper/SortMedia.php +++ b/application/src/View/Helper/SortMedia.php @@ -9,7 +9,7 @@ public function __invoke($files = null) { $sortedMedia = []; $whitelist = ['image/bmp', 'image/gif', 'image/jpeg', 'image/png', 'image/svg+xml', 'image/webp', 'video/flv', 'video/x-flv', 'video/mp4', 'video/m4v', - 'video/webm', 'video/wmv', 'video/quicktime', 'application/pdf', ]; + 'video/webm', 'video/wmv', 'video/quicktime', 'application/pdf', ]; $html5videos = []; $mediaCount = 0; @@ -23,6 +23,10 @@ public function __invoke($files = null) $sortedMedia['lightMedia'][$mediaCount]['tracks'] = []; } $mediaCount++; + } elseif (strpos($mediaType ?? '', 'image/') === 0 && $media->hasThumbnails()) { + $sortedMedia['lightMedia'][$mediaCount]['media'] = $media; + $sortedMedia['lightMedia'][$mediaCount]['forceThumbnail'] = true; + $mediaCount++; } else { $sortedMedia['otherMedia'][] = $media; } diff --git a/application/view/common/resource-page-block-layout/lightbox-gallery-media.phtml b/application/view/common/resource-page-block-layout/lightbox-gallery-media.phtml index da89929c5b..fc1f0d0d94 100644 --- a/application/view/common/resource-page-block-layout/lightbox-gallery-media.phtml +++ b/application/view/common/resource-page-block-layout/lightbox-gallery-media.phtml @@ -1,5 +1,9 @@ mediaType(); -?> -lightGalleryOutput([['media' => $media]]); ?> +$sortedMedia = $this->sortMedia([$media]); + +if (isset($sortedMedia['lightMedia'])): + echo $this->lightGalleryOutput($sortedMedia['lightMedia']); +else: + echo $this->partial('common/resource-page-block-layout/media-render'); +endif;