Hello! I try to add a placeholder image to a video file.
I don’t understand how to target the image though.
Thanks in advance!
This is my code:
$video = $block->cardvideo()->toFile();
if ($block->cardvideo()->image()->exists()) {
$image = $block->cardvideo()->image()->toFile();
$alt = $alt ?? $image->alt();
}
How is the image that corresponds with the video selected?
It is a file section inside the files/video.yml template. That is where the trouble starts, I guess…
Yes, you would have to assign a particular placeholder image to a video unless you can match them by filename.
I guess you are using a custom block for a local video? Then it would make sense to include a files field where you can select an image. Or in the file blueprint for the video.
A files section doesn’t make much sense.
The blueprint of the video looks like this. Its a tweak of the original.
You can choose a local video or youtube / vimeo, but this question only applies to the local video.
Blueprint:
title: Video
accept: video/mp4
columns:
- width: 1/2
sections:
content:
type: fields
fields:
image:
layout: list
width: 4/4
label: Poster Image
type: files
max: 1
uploads:
template: blocks/image
caption:
label: Caption
type: textarea
size: medium
- width: 1/2
sections:
meta:
type: fields
fields:
alt:
label: Alternative Text
type: text
photographer:
label: Photographer
type: text
width: 2/3
license:
label: License
type: select
width: 1/3
options:
- Unsplash
- CC BY 4.0
- CC BY-SA 4.0
- CC BY-NC 4.0
- CC BY-ND 4.0
link:
label: Link
type: url
colorextractor:
label: Color extractor
type: colorextractor
The placeholder img is stored under ‘image’. That part seems to work.
The trouble is here probably:
$image = $block->cardvideo()->image()->toFile();
$posterImage = ( $video = $block->cardvideo()->toFile() ) ? $video->content()->get('image')->toFile() : null;
if ($posterImage) {
// do stuff
}
This is great! Thank you!!!