Use queue for adding matrix entries#15999
Merged
brandonkelly merged 7 commits into5.xfrom Nov 7, 2024
Merged
Conversation
brandonkelly
reviewed
Nov 5, 2024
…tps://github.com/craftcms/cms into bugfix/cms-15980-matrix-initial-field-not-loaded
[ci skip]
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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
minEntriessetting 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 aminEntriesof 1. That field is nested within another matrix field (mx2) which has aminEntriesof 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.addEntrycalls for themx2field, one for each of theminEntriesit requires. Each of those calls will return additional JS that will execute their ownMatrixInput.addEntrycalls to satisfy theminEntriessettings of their children. All of these calls will also trigger asave-draftcall 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
.addEntrycalls into the queue (wheresave-draftalready lives) the requests stay more orderly.An infinite loop example of the new code
CleanShot.2024-11-01.at.13.48.08.mp4