From 73981caf0774ab88041ef805835fc386c9a5fe00 Mon Sep 17 00:00:00 2001 From: Dallas Lu <914202+dallaslu@users.noreply.github.com> Date: Wed, 25 Aug 2021 14:33:13 +0800 Subject: [PATCH] Set thumb image as the attribute poster of video automatically --- .../Common/Media/Traits/VideoMediaTrait.php | 20 +++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/system/src/Grav/Common/Media/Traits/VideoMediaTrait.php b/system/src/Grav/Common/Media/Traits/VideoMediaTrait.php index e03fbbd81..e12b6a19b 100644 --- a/system/src/Grav/Common/Media/Traits/VideoMediaTrait.php +++ b/system/src/Grav/Common/Media/Traits/VideoMediaTrait.php @@ -9,6 +9,9 @@ namespace Grav\Common\Media\Traits; +use Grav\Common\Page\Medium\MediumFactory; +use Grav\Common\Page\Medium\ThumbnailImageMedium; + /** * Trait VideoMediaTrait * @package Grav\Common\Media\Traits @@ -51,14 +54,27 @@ public function playsinline($status = false) /** * Parsedown element for source display mode * - * @param array $attributes - * @param bool $reset + * @param array $attributes + * @param bool $reset * @return array */ protected function sourceParsedownElement(array $attributes, $reset = true) { $location = $this->url($reset); + if (!isset($attributes['poster']) || ($attributes['poster'] !== 0 && $attributes['poster'] !== '0')) { + if ($this->thumbnailExists('page')) { + $thumb = $this->get("thumbnails.page", false); + if ($thumb) { + $thumb = $thumb instanceof ThumbnailImageMedium ? $thumb : MediumFactory::fromFile($thumb, ['type' => 'thumbnail']); + $attributes['poster'] = $thumb->url(); + } + } + } + if (isset($attributes['poster']) && ($attributes['poster'] === 0 || $attributes['poster'] === '0')) { + unset($attributes['poster']); + } + return [ 'name' => 'video', 'rawHtml' => 'Your browser does not support the video tag.',