1

I'm going through my current project and making my loops more "eager". It's working great in several areas and improving performance massively but for some reason I can't get it to work when the entries I'm asking for have a dropdown field. "newsType" is the name of the field. Am I missing some obvious mistake here or are dropdowns a special case somehow?

The error:

Array to string conversion
echo twig_escape_filter($this->env, $this->getAttribute($context["article"], "newsType", array()), "html", null, true);

The code:

{% set latestNews = {
    section : 'news',
    with : ['title', 'body', 'newsType'],
    limit : '6'
} %}

{% if latestNews|length %}
{% for article in craft.entries(latestNews) %}
    <div class="newsEl {{ article.newsType }}">
    <h2>{{ article.newsType.label }}</h2>
    <article>
       <h3>{{ article.title }}</h3>
         <p>
           {{ article.body|striptags|slice(0, 150) ~ '...' }}
         </p>
        </article>
        <a href="{{ article.url }}" class="readMore">Read More <span>&rarr;</span></a>
    </div>
{% endfor %}
{% endif %}
  • Just read in a comment here that eager loading only works with relational elements. So it's pointless to put a dropdown field in "with." Is this correct? If that's the case should I even be doing it for "title" and "body"? – James M Nov 20 '17 at 0:54
  • Nope, you shouldn't be doing it for title or body either, assuming those are not relations to other Elements. title definitely won't be; body might be if it's a Matrix or some other Element. – andrew.welch Nov 20 '17 at 0:55
1

Dropdowns are not relations, and therefore they don't need to be eager loaded (and indeed can't be eager loaded).

Here's an explanation of what's going on under the hood with eager loading: Speed up your Craft CMS Templates with Eager Loading

TL;DR only things that are relations, which are other Elements are candidates for eager loading. Other fields like Dropdowns, etc. are loaded as part of the content already.

|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.