<p>Asset fields return like an array so they can potentially have more than one item, you need to request the first item only with <code>.one()</code> (as opposed to requesting <code>.all()</code>)</p>
<pre><code>//First check if the asset field has more than zero items
{% if block.video.videoFileWebm|length %}
    // Retrieve the first and only item in the asset field
    &lt;source src=&quot;{{ block.video.videoFileWebm.one().url }}&quot; type=&quot;video/webm&quot;&gt;
{% endif %}
</code></pre>
<p>I would also set <code>block.video</code> to a variable to clean up the code a bit and maybe speed it up a little.</p>

CC BY-SA 4.0
2020-09-28T13:39:10.197Z
It's not really accurate to say that "asset fields presume that you may have more than one item", it's more the case that element queries need to be _executed_ - whether that's `.one()` or `.all()` or `.ids()` or any of the other execution methods.
James Smith
CC BY-SA 4.0
2020-09-28T13:53:08.687Z
@JamesSmith you're right, that was badly written, updated!
dmatthams
CC BY-SA 4.0
2020-09-28T14:03:12.233Z
Sorry for splitting hairs, but still not quite right - Calling an asset field without one of the execution methods will not return an array - it'll return an _element query object_: that query then needs to be executed before you can access the result - which in this case would be an Asset object.
James Smith
CC BY-SA 4.0
2020-09-28T14:23:13.953Z
Hey, I'm happy with it – it's easy to understand for all levels, but feel free to edit if you want.
dmatthams
CC BY-SA 4.0
2020-09-28T13:26:20.590Z
2020-09-28T14:21:37.917Z
2020-09-28T14:21:37.917Z
dmatthams
<p>You haven't executed your element queries. You'll need to call <code>.one()</code> on each related element, or, better, use eager loading to do the whole lot in one go:</p>
<p>I've assumed in the example below that 'video' is an entry select field type inside a matrix block type called 'video' inside a matrix field called 'matrixBlocks':</p>
<pre><code>{% set matrixBlocks = entry.matrixBlocks.with([    
    'video:video.videoThumbnail',
    'video:video.videoFileWebm',
    'video:video.videoFileOgg',
    'video:video.videoFileMp4',
]).all() %}

{% for block in matrixBlocks %}
    {% if block.type == 'video' and block.video|length %}
        {% set videoEntry = block.video[0] %}
        &lt;video {{ videoEntry.videoAutoplay ? 'autoplay' }} {{ videoEntry.videoLoop ? 'loop'}}
            {% if videoEntry.videoThumbnail %}
                {% set optimizedImages = videoEntry.videoThumbnail[0].imageVariants16by9 %}
                poster=&quot;{{ optimizedImages.srcset() }}&quot;
            {% endif %} muted&gt;

            {% if videoEntry.videoFileWebm %}
                &lt;source src=&quot;{{ videoEntry.videoFileWebm[0].url }}&quot; type=&quot;video/webm&quot;&gt;
            {% endif %}
            {% if videoEntry.videoFileOgg %}
                &lt;source src=&quot;{{ videoEntry.videoFileOgg[0].url }}&quot; type=&quot;video/ogg&quot;&gt;
            {% endif %}
            {% if videoEntry.videoFileMp4 %}
                &lt;source src=&quot;{{ videoEntry.videoFileMp4[0].url }}&quot; type=&quot;video/mp4&quot;&gt;
            {% endif %}
            
            &lt;p&gt;This browser does not support the video element.&lt;/p&gt;
        &lt;/video&gt;
    {% endif %}
{% endfor %}
</code></pre>

CC BY-SA 4.0
2020-09-28T15:41:43.223Z
2020-09-28T15:41:43.223Z
James Smith
<p>I'm trying to output the video files from within a block but get a <code>Calling unknown method: craft\elements\db\AssetQuery::url()</code> error when using the following:</p>
<pre><code>{{ block.video.videoFileWebm.url }}
</code></pre>
<p>Assets in this volume have public URLs so I'm not sure why this isn't working.</p>
<p><strong>FULL CODE</strong></p>
<pre><code>{% if block.video %}

                &lt;video {% if block.video.videoAutoplay %}autoplay{% endif %} {% if block.video.videoLoop %}loop{% endif %} 
                                                    
                    {% if block.video.videoThumbnail %}
                    {% set optimizedImages = block.video.videoThumbnail.one().imageVariants16by9 %}
                    poster=&quot;{{ optimizedImages.srcset() }}&quot;
                    {% endif %} muted&gt;

                    {% if block.video.videoFileWebm %}
                        &lt;source src=&quot;{{ block.video.videoFileWebm.url }}&quot; type=&quot;video/webm&quot;&gt;
                    {% endif %}
                    {% if block.video.videoFileOgg %}
                        &lt;source src=&quot;{{ block.video.videoFileOgg.url }}&quot; type=&quot;video/ogg&quot;&gt;
                    {% endif %}
                    {% if block.video.videoFileMp4 %}
                        &lt;source src=&quot;{{ block.video.videoFileMp4.url }}&quot; type=&quot;video/mp4&quot;&gt;
                    {% endif %}
                    
                    &lt;p&gt;This browser does not support the video element.&lt;/p&gt;
                &lt;/video&gt;

            {% endif %}
</code></pre>

CC BY-SA 4.0
2020-09-28T13:00:07.947Z
Just to clarify, is "video" a block type in a matrix, or is it a matrix field in an entry? I'm confused because you're doing `block.video` when it looks like you mean `entry.videoBlocks`, more specifically `entry.videoBlocks.one()`?
James Smith
CC BY-SA 4.0
2020-09-28T15:30:05.047Z
...Or is "video" an entries select field in a matrix block? Either way you need to add some eager loading. Check your deprecation errors in Utilities and you'll see what needs fixing.
James Smith
<craft3><assets><video>
Video Asset URLs not rendering
# Video Asset URLs not rendering

(Asked by: username_0 on 2020-09-28)

I'm trying to output the video files from within a block but get a Calling unknown method: craft\elements\db\AssetQuery::url() error when using the following:
{{ block.video.videoFileWebm.url }}

Assets in this volume have public URLs so I'm not sure why this isn't working.
FULL CODE
{% if block.video %}

                

                    {% if block.video.videoFileWebm %}
                        
                    {% endif %}
                    {% if block.video.videoFileOgg %}
                        
                    {% endif %}
                    {% if block.video.videoFileMp4 %}
                        
                    {% endif %}
                    
                    This browser does not support the video element.
                

            {% endif %}

**Tags:** craft3, assets, video

> **Comments:**
> **username_1**: Just to clarify, is "video" a block type in a matrix, or is it a matrix field in an entry? I'm confused because you're doing `block.video` when it looks like you mean `entry.videoBlocks`, more specifically `entry.videoBlocks.one()`?  
> **username_1**: ...Or is "video" an entries select field in a matrix block? Either way you need to add some eager loading. Check your deprecation errors in Utilities and you'll see what needs fixing.  

---

## 2 answers

### Answer by username_2 on 2020-09-28. Score: 3 | Accepted answer

Asset fields return like an array so they can potentially have more than one item, you need to request the first item only with .one() (as opposed to requesting .all())
//First check if the asset field has more than zero items
{% if block.video.videoFileWebm|length %}
    // Retrieve the first and only item in the asset field
    
{% endif %}

I would also set block.video to a variable to clean up the code a bit and maybe speed it up a little.

> **Comments:**
> **username_1**: It's not really accurate to say that "asset fields presume that you may have more than one item", it's more the case that element queries need to be _executed_ - whether that's `.one()` or `.all()` or `.ids()` or any of the other execution methods.  
> **username_2**: @JamesSmith you're right, that was badly written, updated!  
> **username_1**: Sorry for splitting hairs, but still not quite right - Calling an asset field without one of the execution methods will not return an array - it'll return an _element query object_: that query then needs to be executed before you can access the result - which in this case would be an Asset object.  
> **username_2**: Hey, I'm happy with it – it's easy to understand for all levels, but feel free to edit if you want.  

---

### Answer by username_1 on 2020-09-28. Score: 0

You haven't executed your element queries. You'll need to call .one() on each related element, or, better, use eager loading to do the whole lot in one go:
I've assumed in the example below that 'video' is an entry select field type inside a matrix block type called 'video' inside a matrix field called 'matrixBlocks':
{% set matrixBlocks = entry.matrixBlocks.with([    
    'video:video.videoThumbnail',
    'video:video.videoFileWebm',
    'video:video.videoFileOgg',
    'video:video.videoFileMp4',
]).all() %}

{% for block in matrixBlocks %}
    {% if block.type == 'video' and block.video|length %}
        {% set videoEntry = block.video[0] %}
        

            {% if videoEntry.videoFileWebm %}
                
            {% endif %}
            {% if videoEntry.videoFileOgg %}
                
            {% endif %}
            {% if videoEntry.videoFileMp4 %}
                
            {% endif %}
            
            This browser does not support the video element.
        
    {% endif %}
{% endfor %}


---

