3

This is very nearly a duplicate question but I can't figure out where to insert .all() to execute my element query. I'm still asking because I may be messing up php, function or filter syntax somewhere. I've looked through other related answers (1, 2).

I traced the error to the template(s) below. The first _entry.twig chooses a layout based on url parameters and Admin CP settings:

{% block content %}
  {% if craft.app.request.segments|length == 3 %}
    {% set entry = craft.entries().slug(slug|literal).one() %}
    {% include "shared/_grid-item" with {
      entry: entry,
      index: number,
    } only %}

  {% elseif entry.galleryShowThumbnailPage|length %}
    {% include "shared/_grid" with { entry: entry } %}

  {% else %}
    {% include "shared/_gallery" with { entry: entry } %}
  {% endif %}
{% endblock %}

I'm fairly sure the error is not coming from _entry.twig but may be caused by the variable entry in the shared/_grid-item.twig partial it gets passed to:

{% set images = entry.galleryImages.withTransforms(['sideScrollImage', 'tinyThumb']).all() %}
{% set itemImage = images[index - 1] %}
{% set itemImages = []|merge([itemImage]) %}
{% if itemImage.relatedImages.exists() %}
  {% set itemImages = itemImages|merge(itemImage.relatedImages) %}
{% endif %}

{% set next = entry.url ~ '/' ~ (index + 1) %}
{% set prev = entry.url ~ '/' ~ (index - 1) %}
{% if index == 1 %}{% set prev = false %}{% endif %}
{% if index == (images|length) %}{% set next = false %}{% endif %}

{% extends "_layouts/_default.twig" %}

{% block content %}
    <main role="main" class="content">
      <h1 class="content__title content__title--hidden">
        {{ entry.title }}
      </h1>

      {% include "_partials/_sideScroll" with {
        back: entry.title,
        images: itemImages,
        next: next,
        prev: prev,
      } only %}
    </main>
  {% cache %}
  {% endcache %}
{% endblock %}

My understanding of the element query is that I call a for loop but then execute it by calling .all(). I'm not sure if any of the code in _grid-item.twig would call a for loop I'm unaware of – maybe in {% set itemImages = []|merge([itemImage]) %} or {% set itemImages = itemImages|merge(itemImage.relatedImages) %}? Does some of the php syntax need to change to allow adding .all()?

I've tried adding .all() various places like {% set itemImage = images.all()[index - 1] %} but everything throws a twig runtime error.

2
  • What kind of field is “entry.galleryShowThumbnailPage”? Commented Mar 25, 2022 at 17:22
  • entry.galleryShowThumbnailPage is a checkbox field type
    – Yawner
    Commented Mar 25, 2022 at 17:24

1 Answer 1

3

The issue is likely this line:

{% set itemImages = itemImages|merge(itemImage.relatedImages) %}

The |merge filter treats the relatedImages field as an array, which is why you get that deprecation error.

So to fix it, add the .all() call to execute the query (which will return an array), before merging:

{% set itemImages = itemImages|merge(itemImage.relatedImages.all()) %}

A tip that could make it easier to track down issues like these in the future, is to enable devMode, and add this to your config/app.php config file:

return [
    'components' => [
        'deprecator' => [
            'throwExceptions' => YII_DEBUG,
        ],
    ],
];

That will make Craft throw an actual PHP exception for deprecated code, with a full stack trace (instead of just logging the deprecation), which can make it easier to debug this stuff.

1
  • 1
    Awesome thanks! Works like a charm. I've enabled the deprecator in my config. It was really tough to find which templates were causing the issue in the stack trace before.
    – Yawner
    Commented Mar 25, 2022 at 17:37

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.