3

I have set up a Matrix field that contains a block type that contains among other things, an asset field. Trying to iterate through the contents of that matrix field works just fine for all other field types but produces the following error when it comes across the asset field type.

Object of class Craft\ElementCriteriaModel could not be converted to string

This is the part of the template that produces the error. "icon" is the asset field type.

{% for block in entry.visitorContentBuilder %}

    {% if block.type == "listItemWithIcon" %}

        {{ block.icon }}

    {% endif %}

{% endfor %}

1 Answer 1

2

You'll want to use block.icon.first() if you just want the first Asset, or you could loop through all Assets:

{% for block in entry.visitorContentBuilder %}

    {% if block.type == "listItemWithIcon" %}

        <ul>
            {% for icon in block.icon %}
                <li>{{ icon }}
            {% endfor %}
        </ul>

    {% endif %}

{% endfor %}

See here for a description of the difference between an ElementCriteriaModel and actual fetched elements in Craft.

1
  • Thank you! That solved it for me. Also, in this particular case {{block.icon.first.url}} produced the path that I was actually looking for. I wish there was some sort of "default" output for {{ block.icon }} instead of an error. Commented Aug 2, 2017 at 12:24

Your Answer

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

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