0

I have an Entry field (page) as part of my Matrix field (footer).

When the client selects 5 entries, I want to display them all in my list and not just the first. I can only seem to get my code working with .first or .last. Everything else I try gives me the old "Object of class Craft\ElementCriteriaModel could not be converted to string" error.

<ul class="footlist">
    <li class="foottitle">{{ block.navSection }}</li>
        {% if block.type == "footerCategory" %}
        {% set page = block.page.first() %}
           <li>{{ page.title }}</li>
        {% endif %} 
</ul>

My matrix for this list: enter image description here

  • What happens if you remove the first()? – Damon Oct 20 '17 at 3:53
  • I get the error when i remove first – kingkelly Oct 20 '17 at 16:35
1

I think you'll need to remove the first() part. Then your twig will look something like this...

<ul class="footlist">
    <li class="foottitle">{{ block.navSection }}</li>
        {% if block.type == "footerCategory" %}
            {% set pages = block.page %}
            {% if pages|length %}
                {% for page in pages %}
                    <li>{{ page.title }}</li>
                {% endfor %}
            {% end if %}
        {% endif %}
    </li> 
</ul>
  • Holy smokes that worked! Thanks! Didnt think i would need that extra if and for, but it works! – kingkelly Oct 20 '17 at 16:34
1

The elementCriteriaModel is a class/object that handles your search. It's a class that combines multiple methods to fetch entries from your database with an iterable interface on it.

That means: when you receive an entry you get all the queries you need attached to it to pull all the relationship instead of loading all the other entries all the time. So entry.matrix is nothing but database command to pull the other entries which makes craft fast. Otherwise you would take much more time.

To make it short: when you loop through your elementCriteriaModel the method .find() is executed, that fetches all your entries and return them as an array. The method first() returns only the first element, last() returns only the last element.

In order to pull all you just need to loop block.page {% for entry in block.page %} will to it

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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