Skip to content
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

Set thumb image as the attribute poster of video automatically #3429

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
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
20 changes: 18 additions & 2 deletions system/src/Grav/Common/Media/Traits/VideoMediaTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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')) {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It might be simplified in this way:

Suggested change
if (!isset($attributes['poster']) || ($attributes['poster'] !== 0 && $attributes['poster'] !== '0')) {
$this->setPoster($attributes);
$this->removePoster($attributes);

To make it work, you need to create the following methods in the VideoMediaTrait trait:

    private function isPosterUnknown(array $attributes): bool
    {
        return !isset($attributes['poster']) || (int) $attributes['poster'] !== 0;
    }

    private function removePoster(array $attributes): void
    {
        if ($this->isPosterUnknown($attributes)) {
            return;
        }

        unset($attributes['poster']);
    }

    private function setPoster(array $attributes): void
    {
        if ($this->isPosterUnknown($attributes) && $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();
            }
        }
    }

The names of new methods may be different. I want to show you the idea. Of course, you'll need to double-check that and ensure it works as expected.

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' => '<source src="' . $location . '">Your browser does not support the video tag.',
Expand Down