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 %}
Brad Bell
  • 67,440
  • 6
  • 73
  • 143
Panos Spiliotis
  • 559
  • 4
  • 12

1 Answers1

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.

Brad Bell
  • 67,440
  • 6
  • 73
  • 143
  • 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. – Panos Spiliotis Aug 02 '17 at 12:24