Skip to content

Commit

Permalink
Render non-browser-native images in gallery
Browse files Browse the repository at this point in the history
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)
  • Loading branch information
zerocrates committed Jan 9, 2025
1 parent af29fd9 commit 8e981a7
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 17 deletions.
48 changes: 35 additions & 13 deletions application/src/View/Helper/LightGalleryOutput.php
Original file line number Diff line number Diff line change
Expand Up @@ -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' => [
Expand All @@ -60,14 +61,35 @@ public function __invoke($files = null)
}
}
$videoSrcJson = json_encode($videoSrcObject);
$html .= '<div data-video="' . $escape($videoSrcJson) . '" ' . $mediaCaptionAttribute . 'data-thumb="' . $escape($media->thumbnailUrl('medium')) . '" data-download-url="' . $source . '" class="media resource">';
$attribs['data-video'] = $videoSrcJson;
} elseif ($mediaType == 'application/pdf') {
$html .= '<div data-iframe="' . $escape($source) . '" ' . $mediaCaptionAttribute . 'data-src="' . $source . '" data-thumb="' . $escape($media->thumbnailUrl('medium')) . '" data-download-url="' . $source . '" class="media resource">';
$attribs['data-iframe'] = $source;
$attribs['data-src'] = $source;
} else {
$html .= '<div data-src="' . $source . '" ' . $mediaCaptionAttribute . 'data-thumb="' . $escape($media->thumbnailUrl('medium')) . '" data-download-url="' . $source . '" class="media resource">';
$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 .= '<div';
foreach ($attribs as $attribName => $attribValue) {
$html .= ' ' . $attribName . '="' . $escape($attribValue) . '"';
}
$html .= $media->render();
$html .= '</div>';
$html .= '></div>';
}
$html .= '</div>';

Expand Down
6 changes: 5 additions & 1 deletion application/src/View/Helper/SortMedia.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
<?php
$media = $resource;
$mediaType = $media->mediaType();
?>
<?php echo $this->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;

0 comments on commit 8e981a7

Please sign in to comment.