Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[4.x]: Relational fields are cleared out when changing entry types #12592

Open
mmikkel opened this issue Jan 28, 2023 · 6 comments · Fixed by #12607
Open

[4.x]: Relational fields are cleared out when changing entry types #12592

mmikkel opened this issue Jan 28, 2023 · 6 comments · Fixed by #12607
Assignees

Comments

@mmikkel
Copy link
Contributor

mmikkel commented Jan 28, 2023

What happened?

Description

If the entry type is changed for an entry, relations made in a relational field that doesn't exist in the new entry type's field layout, are removed.

This is inconsistent with how Craft retains data for all other field types (plaintext, select fields, etc) when changing an entry's entry type. It's also not how this used to work (I'm not able to reproduce the behavior on Craft 3.7.63.1).

I'm unsure if the current behavior is an intentional change in Craft 4, or a bug?

One interesting detail: From looking at the relations table, it doesn't look like the relations are deleted when the entry type is first changed to the type without the relational field; it's only when the entry type is changed back to the type with the relational field, that the relations are actually deleted from the database. Don't know if that's relevant or not, but seemed a bit odd to me.

Steps to reproduce

  1. Create a section with two entry types, and add a relational field (assets, entries, Matrix ) to one of them.
  2. Create an entry and add an element to the relational field. Save the entry.
  3. Change the entry type to the one without the relational field. Save the entry.
  4. Change the entry type back to the type with the relational field. Notice that that the relational field is now empty.

Expected behavior

Existing relations made in fields that are no longer in an element's field layout after changing entry types should be retained, similar to how Craft retains data for all other field types in the same situation.

Actual behavior

Relations are deleted.

Craft CMS version

4.3.6.1

PHP version

8.1.12

Operating system and version

macOS 12.6

Database type and version

10.9.3-MariaDB

Image driver and version

No response

Installed plugins and versions

None

@brandonkelly
Copy link
Member

It’s most likely a side effect how entry type switching works in Craft 3 vs 4. In Craft 3 there was a dedicated entries/switch-entry-type action which applied the new entry type (and field layout) to the entry in memory, and returned the new tab/field input HTML, without actually saving anything immediately. Whereas in Craft 4, switching the entry type is just another content change as far as the element editor is concerned, and will trigger an autosave.

We’ll dig into this a bit more out of curiosity, but my guess is that the relations aren’t getting copied over during that type change autosave, as they weren’t in the previous field layout, and then end up getting deleted when the provisional draft is applied.

@brianjhanson
Copy link
Contributor

I tracked this down to an order of operations bug. We're currently calling _applyParamsToElement() after we create a draft. When the draft is created by changing the type, this causes the initial draft to be created with the previous typeId. That results in the wrong fieldLayout getting loaded and bad values getting returned from getFieldValues().

I've opened a PR to adjust this which (pending any unexpected issues) should make its way into the next release.

@brandonkelly
Copy link
Member

Fixed for the next release via #12607!

@mmikkel
Copy link
Contributor Author

mmikkel commented Feb 3, 2023

Awesome, thanks a lot for the quick fix!

@brandonkelly
Copy link
Member

Craft 4.3.7 has been released with that fix.

brandonkelly added a commit that referenced this issue Feb 5, 2023
@brandonkelly
Copy link
Member

That change was reverted in Craft 4.3.7.1 as it was the culprit for #12623.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
3 participants