I have a custom module which uses the EVENT_AFTER_SAVE event to update some fields, it appears that in 3.6 using Craft::$app->elements->saveElement($entry); seems to make matrix blocks disappear in the field when publishing a draft entry.
Just resaving the entry and doing nothing else makes the matrix blocks disappear.
The content still seems to be in the database table, but on resave they seem to disappear.
public static $entryUpdatingAfterSave = false;
Event::on(
Entry::class,
Entry::EVENT_AFTER_SAVE,
function (ModelEvent $event) {
// Get entry
$entry = $event->sender;
// Don't worry about drafts or revisions
if (ElementHelper::isDraftOrRevision($entry))
{
return;
}
if (!self::$entryUpdatingAfterSave)
{
self::$entryUpdatingAfterSave = true;
/**
* Resave entry
*/
$success = Craft::$app->elements->saveElement($entry);
if (!$success)
{
Craft::error('Couldn鈥檛 save the entry "'.$entry->title.'"', __METHOD__);
}
// Reset
self::$entryUpdatingAfterSave = false;
}
}
);
I鈥檓 not able to reproduce this, using the exact same code. The entry saves again without any blocks going missing.
Do you have any plugins installed?
I've tested this with all plugins disabled, and the module only containing the code in the example.
I double checked to make sure nothing else was going on, and still the same thing with my current setup.
A recording on what I see between the draft and publish. As drafts and revisions are ignored from being resaved.
https://user-images.githubusercontent.com/1436378/106145831-e66a7c80-616d-11eb-9142-0331cb530737.mp4
My bad, I missed that this was in the context of publishing a draft. Able to reproduce now.
Think that was my fault for not specifying it
The issue is that when that event fires, in the context of publishing a draft, the Matrix blocks haven鈥檛 actually been propagated over to the main entry yet.
If you switch to EVENT_AFTER_PROPAGATE, this will work as expected. Nothing else needs to change.
Using EVENT_AFTER_PROPAGATE did fix this issue.
This was highlighted because of Craft 3.6 workflow change making cmd+s save as a draft, rather than saving an entry.
Thanks for your help @brandonkelly
@brandonkelly I've tested over this a couple of times and found that when I use EVENT_AFTER_PROPAGATE with a section that doesn't have a matrix field it does not resave entries. Is this the expected behaviour?
If I am looking to find a solution in the scenario when I am publishing an entry with or without a matrix field (and potentially a Neo or Supertable field), that it will resave.
I would have expected EVENT_AFTER_PROPAGATE to run even when I don't have a matrix field, are there some checks going on for this?
My current solution is to call the resave method in EVENT_AFTER_SAVE and EVENT_AFTER_PROPAGATE within each event I am checking to see if the entry contains a matrix field to know if it should be called on that event or not.
E.G. if matrix field, then don't run on EVENT_AFTER_SAVE, but run on EVENT_AFTER_PROPAGATE
@zizther That event will trigger regardless of whether there are any Matrix fields. Maybe there is a validation error or something, which is preventing them from being saved the second time?
Most helpful comment
Using
EVENT_AFTER_PROPAGATEdid fix this issue.This was highlighted because of Craft 3.6 workflow change making cmd+s save as a draft, rather than saving an entry.
Thanks for your help @brandonkelly