field layout elements and deleted fields
Description
Issue: Field layout elements with non-existent fields are still being added to the layout, causing them to never get deleted from the project config.
Steps to reproduce:
- clean 5.8.18 installation
- create two plain text fields
- create a section with an entry type that uses both those fields and the default title field
- check the project config file for the entry type and note that it has layout elements for the title field and both plain text fields
- delete one of the plain text fields via control panel > Settings > Fields (do not edit the entry type at this point)
- check the project config file for the entry type and note that it has layout elements for the title field and both plain text fields; the layout element for the deleted field will not have the comment with the field’s name next to
fieldUidvalue - edit the entry type, make no changes and save
- check the project config file for the entry type and notice that nothing has changed - it still has 3 layout elements in it
When we get the field layout, we create tabs for it, and each tab gets all the elements it’s supposed to have.
Up until 5.8.12, when Fields->createLayoutElement() was called from FieldLayoutTab->setElements(), the FieldNotFoundException was thrown if the field couldn’t be found from the CustomField->setFieldUid() method.
The changes in https://github.com/craftcms/cms/commit/e80ca1b60bf4daa8958e6a7d0801ec8ab6f427f0 moved the throwing of the FieldNotFoundException to the CustomField->getField() method, which means it’s never really thrown when rendering, e.g. entry type page with its field layout. This causes the layout element for the ghost field to be included in the field layout, which in turn means that it’s never removed from the project config.
Fix:
When a layout element is created for a CustomField, check that the field exists, and if not, don’t include it in the field layout tab’s elements.
Related issues
caught while working on a Feed Me issue (https://github.com/craftcms/feed-me/issues/1679)