I've got a use case where I'd like to be able to not only upload an image for a blog entry's "featuredThumb" but also have the ability to upload a video. I was originally using animated GIFs but they are huge. So much like twitter I'm going to switch them for videos.
Currently, I have something that I'm sure you'll be very familiar with. I've got an asset field within my blog section that accepts an image and my template code looks like this:
{% for entry in craft.entries.section('blog').find() %}
<article>
<a href="{{ entry.url }}">
<img src="{{ entry.featuredThumb.getUrl("someTransform") }}" alt="{{ entry.featuredThumb.title }}">
</a>
<h3>
<a href="{{ entry.url }}">
{{ entry.title }}
</a>
</h3>
</article>
{% endfor %}
I'd like to be able to switch the image for a small HTML5 video. This poses a few problems, as numerous video formats are required for cross browser compatibility, along with an image as a poster / fallback.
My current solution is to switch the "featuredThumb" asset field, for a new matrix field that contains two block types:
Image
- Asset field - Name: Image
Video
- Asset field - Name: Videos (allows multiple files so the user uploads MP4, OGV, WebM)
- Asset field - Name: Fallback Image
Then in my template update to:
{% for entry in craft.entries.section('blog').find() %}
<article>
<a href="{{ entry.url }}">
{% for block in entry.featuredThumb %}
{% switch block.type %}
{% case "image" %}
<img src="{{ block.image.first().url }}" alt="{{ block.image.first().title }}">
{% case "video" %}
<video width="320" height="180" autoplay loop>
{% for video in block.videos %}
<source src="{{ video.url }}" type="{{ video.getMimeType() }}">
{% endfor %}
<!-- Fallback image for browser without <video> support -->
<img src="{{ block.videos.fallbackImage.first().url }}" alt="{{ block.videos.fallbackImage.first().title }}">
</video>
{% endswitch %}
{% endfor %}
</a>
<h3>
<a href="{{ entry.url }}">
{{ entry.title }}
</a>
</h3>
</article>
{% endfor %}
This is the best solution I'm able to come up with, but it seems a little over-engineered. I'd love to see other people's take on this.
Cheers,
Tom