[4.x]: Relational fields are cleared out when changing entry types
@brianjhanson is already working on this.
Since Jan 30, 2023.
- Dominant language
- PHP
- Stars
- 3.6k
- Forks
- 705
- Avg merge
- 1d 5h
- Merged PRs (30d)
- 134
Description
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
- Create a section with two entry types, and add a relational field (assets, entries, Matrix ) to one of them.
- Create an entry and add an element to the relational field. Save the entry.
- Change the entry type to the one without the relational field. Save the entry.
- 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
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Assessment
This issue has not been assessed yet.