3

I recently upgraded a site from Craft 2 to Craft 3, so I'm trying to figure out what was deprecated and caused this error.

At the top of the page I have this

{% set solutions = craft.entries.section('solutions').level(1).all() %}

And I'm trying to invoke a dropdown menu like so

<li class="nav-item {% if craft.request.segment(1) == 'solutions' %}active{% endif %}">
    {% nav solution in solutions.limit(1) %}
        <a class="nav-link" href="{{ solution.url }}">solutions<span class="sr-only">(current)</span> <i class="icon-downarrow"></i> </a>
    {% endnav %}
    <ul class="submenu">
        {% nav solution in solutions %}
            <li>
                <a href="{{ solution.url }}">{{ solution.title }}</a>
            </li>
        {% endnav %}
    </ul>
</li>

Before I didn't have .all() at the end, but only the first menuitem was showing up. When I added .all() in, I get the error "Impossible to invoke a method ("limit") on an array."

2

craft.entries now returns an ElementQuery. When you add the .all() you're telling Craft to execute the query, which returns an array of Elements.

That means that solutions is now a simple array, and won't have methods like .limit(1) that can be executed on it. It looks like you just want to use the first item in the solutions array, so try changing this:

      {% nav solution in solutions.limit(1) %}
           <a class="nav-link" href="{{ solution.url }}">solutions<span class="sr-only">(current)</span> <i class="icon-downarrow"></i> </a>
      {% endnav %}

To this:

       <a class="nav-link" href="{{ solutions[0].url }}">solutions<span class="sr-only">(current)</span> <i class="icon-downarrow"></i> </a>

More here: {% nav %} Tags

| improve this answer | |

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.