1

I'm trying to loop through images that are eager-loaded inside matrix block, but I can't get the images inside a matrix field handle to loop and show each individual image.

The loop I have loops the correct amount of times (based on how many images there are in each matrix block), but it just shows the first image of the loop.

Here's the set up:

Section handle: products Matrix field handle: imageTextSections Block type handle: sectionBlock Field handle: sectionBlockImages Field type: Assets

I'm trying to loop through images that are eager-loaded inside matrix block, but I can't get the images inside a matrix field to loop and show each individual image.

The loop I have loops the correct amount of times (based on how many images there are in each matrix block), but it just shows the first image of the loop.

Here's the set up:

Section handle: products

Matrix field handle: imageTextSections

Block type handle: sectionBlock

Field handle: sectionBlockImages

Field type: Assets

{% set blocks = entry.imageTextSections.find({
    with: [
        'sectionBlock:sectionBlockImages'
    ]
}) %}

{% for block in blocks %}
...

{% for images in block.sectionBlockImages %}
    {% set image = block.sectionBlockImages[0] ?? null %}
    <img src="{{ image.url }}">
{% endfor %}

{% endfor %}

I figured that the following would work, but I get an error stating that the AssetFileModel.0 is not defined.

{% for images in block.sectionBlockImages %}
    {% set image = images[0] ?? null %}
    <img src="{{ image.url }}">
{% endfor %}

Any guidance would be greatly appreciated!

1
  • What happens when you don't overwrite the asset after you set it correctly? {% for images in block.sectionBlockImages %} should be enough, there is no need to do {% set image = images[0] ?? null %} since your image variable should be the correct asset Jan 19, 2018 at 7:38

1 Answer 1

2

Eager loading an Assets field simply means that instead of block.sectionBlockImages returning an ElementCriteriaModel, it'll return an array. This means that you can't do stuff like block.sectionBlockImages.first(), but have to use brackets notation – i.e. block.sectionBlockImages[0] to access the first Asset.

Looping over the field would look exactly the same, though. So you'll need to change this

{% for images in block.sectionBlockImages %}
    {% set image = block.sectionBlockImages[0] ?? null %}
    <img src="{{ image.url }}">
{% endfor %}

to this

{% for image in block.sectionBlockImages %}
    <img src="{{ image.url }}">
{% endfor %}

...and you should be good.

1
  • I get a deprecation error when doing this method now... Craft 3.5.x - it works, but I would like to do it without the deprecation error....
    – M Smith
    Jan 18, 2021 at 19:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.