Problem/Motivation
I found that components from e.g. hook_entity_extra_field_info() are not removed after the extra field is gone.
(part of this issue text is copied from https://drupal.stackexchange.com/questions/244305/how-to-clean-up-leftov...)
Technical observations
The entries, for both regular fields and extra fields, are stored in EntityDisplayBase::$content.
In the schema, defined in core.entity.schema.yml, we find
core.entity_view_display.*.*.*:
..
mapping:
..
content:
type: sequence
Once an entry is saved there, it stays until something calls $display->removeComponent($name), and saves the display afterwards.
For this, the caller needs to know the name to remove.
I did not find any operation that would remove all unknown components.
Steps to reproduce
(This is the easiest to reproduce, but a bit artificial)
- (Install Drupal with standard profile, export configuration to `./config/sync/`.)
- Edit `config/sync/core.entity_view_display.node.article.default.yml`. Insert a new field display entry, name it 'zzz', and copy the display settings from 'body'.
- Import the modified config (drush cim)
- Visit `/admin/structure/types/manage/article/form-display`. Optionally change display settings for some fields. Save the form.
- Export the config (drush cex)
Expected behavior: The entry for 'zzz' gets removed.
Actual behavior: The entry for 'zzz' stays in the display.
- Download https://drupal.org/project/extra_field (I tried 8.x-1.0-alpha2). (The question is not about this module, it is just a useful example)
- Enable extra_field and extra_field_example modules.
- Visit admin/structure/types/manage/article/display, position the extra fields from the module, and click "Save".
- Uninstall extra_field and extra_field_example modules.
- Visit again admin/structure/types/manage/article/display, click "Save".
- Implement hook_node_view() in a custom module somewhere, and dpm($display->getComponents()) (using devel, obviously). Rebuild the cache to discover the hook. Visit a page where an article node is displayed.
Expected behavior: The components from the extra fields from extra_field_example should be gone now.
Actual behavior: The components still contain the keys from extra_field_example. E.g. "extra_field_all_nodes".
- Declare base fields for an entity type. Either by using a hook, or by changing the entity type definition directly.
- Place the base fields in a view or form display. Save.
- Remove the base fields, by removing the respective code.
- Open the display form, and save it.
- Use `drush cex`, or core config module, or the devel `dpm($display->getComponents())` as above, to inspect the configuration.
Expected behavior: The display components from the removed base fields are gone.
Actual behavior: The display components from the removed base fields are still present in the display configuration.
Proposed resolution
One opportunity to remove the orphan entries would be in EntityDisplayFormBase::submitForm().
More specifically, in EntityDisplayFormBase::copyFormValuesToEntity().
Maybe there could be a message and a checkbox in the field_ui form:
"Orphan entries were detected. Remove orphan entries on form submit?"
Another opportunity would be during cron, perhaps.
If not, there should at least be documentation for a module developer how to clean those entries.
Remaining tasks
User interface changes
API changes
Data model changes
Release notes snippet