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.
entry.galleryShowThumbnailPage
is a checkbox field type