2

I have an Entry Type with a field called Gallery with a handle of gallery. Here are two examples of how I can access the Gallery field in a template:

{% set images = entry.gallery %}

{% set images = craft.assets({
    source:    'images',
    relatedTo: {sourceElement: entry.id, field: 'gallery'},
    order:     'sortOrder'
}) %}

The problem:

The first method works with Live Preview, the second does not. This wouldn't be an issue, but it's my understanding that we have to use the second method to eager load elements. So if I wanted to eager load a relationship on my gallery images, then Live Preview wouldn't work.

Is anyone else running into this issue? Is it a known bug or a misunderstanding of the implementation?

Thanks

Edit

To clarify via Brad's question, the following is the only thing in my template. This does NOT work with Live Preview:

{% set images = craft.assets({
    relatedTo: {sourceElement: entry.id, field: 'gallery'}
}) %}
{% for image in images %}
    {{ image.url }}<br />
{% endfor %}

The entry variable is populated defining the Entry Template in the section settings.

Using the following WORKS with live preview:

{% set images = entry.gallery %}
{% for image in images %}
    {{ image.url }}<br />
{% endfor %}
Peter Tell
  • 1,828
  • 12
  • 19
  • Pretty sure it's a misunderstanding. What's the code for where you initially set entry in the first example. – Brad Bell Jul 01 '16 at 22:13
  • Thanks Brad. Entry comes loaded with the template. It's the one Craft sets based on section settings. – Peter Tell Jul 01 '16 at 22:25
  • Updated my response with full examples Brad. There are no plugins installed and that code is the only thing in my template files. They both render the exact same thing on the front end after the entry is saved as expected. – Peter Tell Jul 01 '16 at 22:41
  • 1
    I tried to replicate this: Created a channel with one gallery field, created an entry, added two images to the field, and in the template fetched them both ways, and looped through the urls. In live preview, both lists of images initially show both urls. But when I add or remove or reorder images from the field, only the list set with entry.gallery gets updated in live preview.

    Confirming that I see a difference too.

    – Marion Newlevant Jul 04 '16 at 20:34
  • 1
    I have this same issue with a matrix field that is eager loaded. – Tom De Smet Apr 07 '17 at 12:11
  • 1
    Yeah, this seems to be a persistent bug. The bummer on this one is you either have to choose between performance or usability or write a bloated conditional in your templates which doesn't seem to be the ideal solution. – Peter Tell Apr 07 '17 at 14:54

1 Answers1

2

As noted in the comments above, eager-loading is not yet compatible with Live Preview. A clean workaround is to disable eager-loading in the template when the page is being viewed in Live Preview:

{% if craft.request.isLivePreview %}
    {% set images = entry.gallery %}
{% else %}
    {% set images = craft.assets({
        relatedTo: {sourceElement: entry.id, field: 'gallery'}
    }) %}
{% endif %}

{% for image in images %}
    {{ image.url }}<br />
{% endfor %}