Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Eager loading and Live Preview #2335

Closed
H-i-red opened this issue Jan 23, 2018 · 9 comments
Closed

Eager loading and Live Preview #2335

H-i-red opened this issue Jan 23, 2018 · 9 comments

Comments

@H-i-red
Copy link

H-i-red commented Jan 23, 2018

Description

Live preview does not seem to work when I "Eager load" a matrix field.

{% set blocks = entry.grid.with([
    ['image:image', {
        withTransforms: ['background']
    }]
]).all() %}

Additional info

  • Craft version: Craft Personal 3.0.0-RC6
  • PHP version: 7.1.6
  • Database driver & version: MySQL 5.6.35
  • Plugins & versions:
@H-i-red
Copy link
Author

H-i-red commented Jan 23, 2018

Workaround !

{% set preview = craft.app.request.isLivePreview %}

{% if preview %}
    {% set blocks = entry.grid.all() %}
{% else %}
    {% set blocks = entry.grid.with([
        ['image:image', {
            withTransforms: ['background']
        }]
    ]).all() %}
{% endif %}

@benface
Copy link
Contributor

benface commented Mar 6, 2018

@brandonkelly This wasn't fixed, was it?

@brandonkelly
Copy link
Member

It is expected behavior, per the Eager-Loading docs:

However when the assets are eager-loaded, entry.assetsField gets overwritten with an array of the eager-loaded assets. So one(), all(), and other element query methods are not available. Instead you must stick to the standard array syntaxes.

(Example uses assets, but the behavior applies to all element types.)

@marionnewlevant
Copy link

I know about the [0] vs .one() difference, but why does eager loading break live preview?

@brandonkelly
Copy link
Member

Sorry, was on a different train of thought I guess 🙃

Live Preview will preload each of your field values based on the POST data, but eager-loading will override those values with the eager-loaded values. So as @H-i-red’s workaround demonstrates, if it’s a Live Preview request you should just avoid eager-loading anything.

@narration-sd
Copy link
Contributor

narration-sd commented Mar 6, 2018

Wow. Is there any way to generally disable eager-loading during Live Preview, or should there be??

@brandonkelly
Copy link
Member

Problem is, if the template is telling Craft to eager-load elements, then the template should also have adjusted following code to treat those field values like arrays (as they will be if they've been eager-loaded. So we can't just disable eager-loading for LP requests, as that will likely cause other errors further down the template.

@narration-sd
Copy link
Contributor

Ok, and I had thought of @H-i-red 's solution before seeing it, which will do what you know I'm interested in, and will have to be standard practice anyway.

Thanks, @brandonkelly

@carlcs
Copy link
Contributor

carlcs commented Jan 3, 2019

There should be a warning in the Eager Loading docs with instruction how to best write the conditional to avoid code duplication.

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants