Cms: Live Preview doesn't update eager-loaded matrix fields

Created on 22 Jun 2017  Â·  7Comments  Â·  Source: craftcms/cms

Description

I'm encountering this bug where eager-loaded matrix fields are not updated in Live Preview. It will always show them the way they were when the entry was first opened.

This means that if my matrix field is empty when the page opens and I then enable Live Preview, the section related to the matrix will always be shown empty in the Live Preview. If I was to have some content in the matrix when the page was opened, then that content would always be shown, whether I remove the block or add a new one.

I've read about the dirty workaround described here: https://craftcms.stackexchange.com/questions/15769/find-not-working-with-live-preview but it really is a pain.

Is it in your short-term plans to work on this issue? Isn't it possible to simply disable eager-loading in Live Preview mode? Wouldn't it make more sense?

Steps to reproduce

  1. In the CP, open an entry where the template contains eager-loaded matrix fields
  2. Open Live Preview
  3. Edit the matrix blocks
  4. The changes won't be visible in Live Preview

Additional info

  • Craft version: 2.6.2982 (Pro)
  • PHP version: 7.0.18-1+deb.sury.org~trusty+1
  • Database driver & version: MySQL 5.5.55-0ubuntu0.14.04.1
  • Plugins & versions:

    • Focal Point Field 1.0.2

    • LJ Cookies 1.0

    • Sprout Fields 2.1.1

    • SuperSort 1.2.0

bug matrix minor

Most helpful comment

We’re planning on implementing https://github.com/craftcms/cms/issues/1787#issuecomment-384762537 in 3.2 or 3.3.

All 7 comments

@brandonkelly do you know if this is also an issue in Craft 3? As well, I would dispute this as being a minor bug, as Live Preview is a pretty big selling point of Craft and having properly-coded Matrix fields not work with it is a big issue (and disabling eager loading would make Live Preview quite slow on complex pages).

@mildlygeeky Yeah almost definitely still an issue in Craft 3. It’s complicated due to the current implementation of Live Preview, otherwise we would have fixed it. But we have a plan that will fix things like this and #2801:

1. make drafts/versions a system-wide thing, where a single “draft” could include more than just one entry/element (#2116)

  1. implement automatic draft creation & auto-draft-saving when editing elements (#1034)
  2. get Live Preview to show the stored draft content rather than unsaved posted content, driven by normal element queries

Hey @brandonkelly. We have a number of large sites running Craft and a huge issue for the teams is that we do not have an auto-save feature while editors are creating entries. Do you have any lead time for when this may be made available?

@stephent2c sorry couldn’t give you a reliable ETA yet; at least 6 months out.

This issue does make for quite a messy templating setup, assuming you don’t want to abandon eager loading and you still want previews to work reliably.

On my current project I’ve ended up with conditional queries like this, which at least gets things working, but it results in loads of deprecation warnings for live previews, since you end up iterating through element queries directly.

To work around that I’ve ended up adding custom |all and |first Twig filters that accept either arrays or element queries as inputs, which is working well so far and keeps the templates fairly clean, but it still feels a bit too hacky, and it’s not great to be relying on custom code for core functionality.

public function allItems($items)
{
    if (is_object($items) && method_exists($items, 'all')) {
        return $items->all();
    }

    return $items;
}

public function firstItem(\craft\web\twig\Environment $env, $items)
{
    if (is_object($items) && method_exists($items, 'one')) {
        return $items->one();
    }

    return twig_first($env, $items);
}

We’re planning on implementing https://github.com/craftcms/cms/issues/1787#issuecomment-384762537 in 3.2 or 3.3.

This has been resolved for the next 3.2 Alpha release!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

michaelhue picture michaelhue  Â·  3Comments

benface picture benface  Â·  3Comments

mccombs picture mccombs  Â·  3Comments

darylknight picture darylknight  Â·  3Comments

angrybrad picture angrybrad  Â·  3Comments