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?
@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)
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!
Most helpful comment
We’re planning on implementing https://github.com/craftcms/cms/issues/1787#issuecomment-384762537 in 3.2 or 3.3.