Use queue for adding matrix entries #15999
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Fixes #15980
For some background, it's important to understand what happens when a matrix field has a
minEntries
setting and is set to inline blocks. In that situation, when the page loads, a bit of javascript will fire off to create the block and save a draft of the current entry. If that matrix field has additional matrix fields within it, the process will be repeated down the tree via JS returned from the initial AJAX request.Now, let's say you have a matrix field (
mx1
) set to inline blocks with aminEntries
of 1. That field is nested within another matrix field (mx2
) which has aminEntries
of 2 and is also set to inline blocks.In a perfect world, you'd end up with:
Unfortunately, when the page loads, the top level matrix field will execute two
MatrixInput.addEntry
calls for themx2
field, one for each of theminEntries
it requires. Each of those calls will return additional JS that will execute their ownMatrixInput.addEntry
calls to satisfy theminEntries
settings of their children. All of these calls will also trigger asave-draft
call on the parent entry.The problem comes in when all these requests get jumbled up. From time to time, the draft will be saved before all the nested entries are ready which causes the nested element editor to clean them up. When that happens and another request expecting that entry to still be around rolls in, things go badly.
By moving the
.addEntry
calls into the queue (wheresave-draft
already lives) the requests stay more orderly.An infinite loop example of the new code
CleanShot.2024-11-01.at.13.48.08.mp4