Skip to content

[5.x]: Nested entries are in the wrong order after resaving section entries via queue job #18109

@mmikkel

Description

@mmikkel

What happened?

Description

After editing certain section settings (or, after applying project config changes to such settings) like the "Entry URI Format", Craft will spin up a ResaveElements queue job to resave that section's entries.

I noticed that after such a job having run, certain entries had some of their Matrix nested entries in the wrong order.

I did some digging, and the culprit turned out to be provisional drafts in the database for the affected owner entries, where the nested entries' content was unchanged, but their sort order was different.

IIRC, Craft doesn't actually duplicate nested entries for drafts, if their content is unchanged. Which I'm guessing is the part of the reason why the sort order from the provisional drafts, somehow becomes applied to the entry's current revision.

Some additional details:

This problem does not happen when entries are resaved via the craft resave CLI command (at least not with the default params). It also doesn't happen for the ResaveElements queue job which is spun up after editing entry type settings, only the one spun up after editing section settings.

To that last point, there is a crucial difference in the two different ResaveElements jobs Craft spins up when editing section and entry types, respectively. The one that appears after editing a section has its criteria attribute configured like this:

"criteria": {
    "sectionId": 4,
    "siteId": [
        1
    ],
    "preferSites": [
        1
    ],
    "unique": true,
    "status": null,
    "drafts": null,
    "provisionalDrafts": null,
    "revisions": null
},

...while the job spun up after editing entry type settings has its criteria attribute configured like this:

"criteria": {
    "typeId": 4,
    "siteId": "*",
    "unique": true,
    "status": null
},

I'm guessing the fact that the section resave job appears to include drafts and revisions is relevant to the problem.

Steps to reproduce

  1. Create a section and add an entry type to it that contains a Matrix field.
  2. Create an entry and two nested entries in the Matrix field. Save the entry.
  3. Change the order of the two nested entries. Let the autosave complete, but do not apply the provisional draft.
  4. Go into the section settings and change the Entry URI Format setting. Save the section.
  5. Craft has now spun up a Resave "Section" entries queue job. Let the queue job complete.
  6. Go back to the entry, and discard the provisional draft. Notice that the change in sort order for the nested entries from the draft, has been applied to the current revision.

Expected behavior

Nested entries' sort order in current/canonical entries should always be retained after resaving entries.

Actual behavior

Craft will sometimes apply the sort order from drafts to the canonical/current entry, when resaving entries via the ResaveElements queue job.

Craft CMS version

5.8.20

PHP version

No response

Operating system and version

No response

Database type and version

No response

Image driver and version

No response

Installed plugins and versions

None

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions