Problem/Motivation
When a config entity implementing EntityWithPluginCollectionInterface is reacting on dependency removal, it should ask the plugins from the collections if they want to take some action when their dependencies are about to be removed.
This issue is adding a generic solution for the one fixed in #2562107: EntityDisplayBase should react on removal of its components dependencies.
Bumping this to critical, because #3326447: Deleting a Media view mode should not result in the deletion of an entire text format combined with #3537624: Deleting filter formats may result in data loss can lead to content data loss. There is a case of this actually happening brought up in slack: https://drupal.slack.com/archives/C1BB308HH/p1755719629686569
Steps to reproduce
Two example scenarios:
- Create a new Image style
- Add an image effect from a module other than the core image module to the image style
- (optional) Add other effects including from the image module as desired
- Save the image style
- Uninstall the module provide the image effect in step 2
- Observe that the image style has been deleted
and From #3326447: Deleting a Media view mode should not result in the deletion of an entire text format
- Install Drupal HEAD with the Standard profile.
- Enable the Media and Media Library modules.
- On
/admin/structure/display-modes/view/add/media/, create a new view mode for Media called "Test" and click save. - On
/admin/structure/media/manage/image/display, under "Custom display settings", check "Test" and click save. - On
/admin/config/content/formats/manage/basic_html, add the media button to the WYSIWYG toolbar. - Further down the page, check "Embed media".
- Further down still, open the "Embed media" vertical tab that becomes available.
- Under "View modes selectable in the 'Edit media' dialog", check "Default" and "Test". Click "Save".
- Go to
/admin/structure/display-modes/view/manage/media.test/delete. It warns you that not only the unused view mode but also the editor config and the basic HTML input format are about to be deleted:
- If you don't read carefully, you may end up deleting the entire Basic HTML format, rendering your content both broken and uneditable in the process.
Proposed resolution
Proposed solution (MR 13171)
- Add new interface
Drupal\Core\Plugin\RemovableDependentPluginInterfacewith methodonCollectionDependencyRemoval() - Add code in
ConfigEntityBase::onDependencyRemoval()that tests if the entity implementsEntityWithPluginCollectionInterface. If so, iterate through collections and if there are plugins that implementRemovableDependentPluginInterfacedependencies::onCollectionDependencyRemoval(), invoke the method on that plugin so it can react by changing its configuration or removing itself from the collection - Add a new enum
RemovableDependentPluginReturnthat has three values:Changed,Remove,Unchanged - If
onCollectionDependencyRemoval()returnsRemoveorUnchanged, thenConfigEntityBase::onDependencyRemoval()will return TRUE, so the config entity's dependencies can be recalculated - If
onCollectionDependencyRemoval()returnsRemove, then remove the plugin from the entity's plugin collection inConfigEntityBase::onDependencyRemoval() - Implement two use cases: 1) prevent image styles from being deleted if a module that provides any of its image effects is uninstalled and 2) prevent filter formats from being deleted if a media entity view mode used in a media embed filter is deleted
- Tests.
(Previous alternate solution)
Add code inConfigEntityBase::onDependencyRemoval()that tests if the entity implementsEntityWithPluginCollectionInterface. If so, iterate through collections and if there are specific plugin dependencies being removed, call the plugin::onDependencyRemoval()method to allow the plugin to react.Add a new methodonDependencyRemoval()in\Drupal\Component\Plugin\DependentPluginInterface.Add emptyonDependencyRemoval()implementations in base classes implementingDependentPluginInterfacewith a simplereturn FALSE;.Implement one use-case.Tests.
Remaining tasks
None.
User interface changes
None.
API changes
Plugin classes can implement new interface Drupal\Core\Plugin\RemovableDependentPluginInterface that has this method:
/**
* Informs the plugin in a collection to act on removal of dependencies.
*
* This method allows a plugin instance in a collection to remove dependencies
* from their configuration. For example, if a plugin integrates with a
* specific module, it should remove that module from its own configuration
* when the module is uninstalled.
*
* The plugin collection and the index of the plugin instance in the
* collection are provided in case the removal of dependencies means that the
* plugin instance should be removed from the collection altogether.
*
* @param \Drupal\Component\Plugin\LazyPluginCollection $collection
* The plugin collection that contains the plugin instance.
* @param array-key $index
* The index of the plugin instance within the collection.
* @param array<string, list<string>> $dependencies
* An array of dependencies that will be deleted keyed by dependency type.
* Dependency types are, for example, entity, module and theme.
*
* @return bool
* TRUE if the configuration of the plugin instance has changed or if the
* plugin instance has been removed from the collection, otherwise FALSE.
*/
public function onCollectionDependencyRemoval(LazyPluginCollection $collection, string|int $index, array $dependencies): bool;
Data model changes
None.
| Comment | File | Size | Author |
|---|---|---|---|
| #70 | reroll_diff_49-70.txt | 9.19 KB | ravi.shankar |
| #70 | 2579743-70.patch | 23.9 KB | ravi.shankar |
| #49 | 2579743-49.patch | 24.78 KB | sam152 |
| #49 | interdiff.txt | 9.73 KB | sam152 |
Issue fork drupal-2579743
Show commands
Start within a Git clone of the project using the version control instructions.
Or, if you do not have SSH keys set up on git.drupalcode.org:
- 2579743-config-entities-implementing
changes, plain diff MR !13171
Comments
Comment #2
claudiu.cristeaComment #3
tim.plunkettComment #4
claudiu.cristeaLet's try a first patch.
Comment #5
yched commentedRight, and most of the code currently in EntityDisplayBase::onDependencyRemoval() & the ::getPluginRemovedDependencies() helper can be moved straight up to ConfigEntityBase::onDependencyRemoval() for that.
But the tricky part is then how each specific ConfigEntity class can react to the plugin::onDependencyRemoval() result (whether "OK, I adapted, ask me for my new settings" or "screw it, I'm not changing myself"). AFAICT that logic is specific to the business logic and implementation of each "config entity with a plugin collection" : EntityDisplays do something, others will need something else.
So it seems ConfigEntityBase::onDependencyRemoval() can provide the general mechnism, but will need to defer a separate submethod to handle the result of plugin::onDependencyRemoval(). And that submethod will likely need to be defined in EntityWithPluginCollectionInterface (it only makes sense if the ConfigEntity is a EntityWithPluginCollectionInterface).
Then, same problem as below with adding a method with existing direct implementations, we'll likely need a new subinterface for that...
Problem is, there are no such base classes ? Most implementations of that interface implement it directly, and we can be sure that that is also the case in contrib/custom code out there, and adding that method would break them.
So I fear this means adding a new subinterface of DependentPluginInterface to receive that new method :-/
Comment #9
xjmThis has come up again in #2830581: Fix ContentModeration workflow type to calculate correct dependencies.
Bumping to major because this is really confusing and it's hard to realize you need to add such a method to your specific entity type interface, and screwing up your entity's dependency management can cause data integrity issues or data loss, so it would be good to find some solution or mitigation for this.
Comment #10
tim.plunkettThis would help resolve #1764380: Merge PluginSettingsInterface into ConfigurableInterface, as we need a replacement for \Drupal\Core\Field\PluginSettingsInterface::onDependencyRemoval()
Comment #11
sam152 commentedHere is an initial version of this. I think the BC concerns are valid, so here is an interface which extends DependentPluginInterface.
I think the meaty tests will come from the implementations of onDependencyRemoval, which are up to the plugins to provide and test. The current test only verifies plugins that implement the interface have a chance to act. Most of the docs on the interface are copied from ConfigEntityInterface::onDependencyRemoval, but should still apply.
Comment #12
tim.plunkettGreat start!
First, due to #2851574: DependentPluginDefinitionInterface and DependentPluginDefinitionTrait should be in Drupal\Core not Drupal\Component we shouldn't repeat the mistakes of core, let's have this live in Drupal\Core.
Second, since PluginSettingsBase already provides implementations of these methods (due to PluginSettingsInterface as mentioned in #10), let's use this new interface there.
Third, the docs on the new RemovableDependentPluginInterface shouldn't mention entities at all. I think this is just because they were copy/pasted as you mentioned.
Finally, we need to untangle \Drupal\Core\Entity\EntityDisplayBase::onDependencyRemoval(). However that may prove trickier than expected, since neither EntityFormDisplay or EntityViewDisplay use EntityWithPluginCollectionInterface correctly...
Here's a patch addressing my first two points.
Comment #13
sam152 commentedIt seemed to make sense to mention their relationship to config entities and how removing dependencies will preserve those entities, but I guess that connection isn't explicit based on the interface.
Fixed the docs and resolved a @todo pointing to this issue. EntityDisplayBase looks like a hairy one, is that in scope for this issue?
Comment #15
claudiu.cristeaThe relevant part from @yched's comment:
EDIT: Short version:
Comment #16
sam152 commentedThanks for summarising that, I think step 1 and 3 are already done in #13.
From what I can tell, getPluginRemovedDependencies is used to detect if a plugin couldn't handle removing it's dependencies. This happens so that the display can just disable the component. I think it's pretty unique for a config entity to understand how to disable some part of itself based on a plugin having unresolved dependencies, it seems like the most most complex use case to the point where I don't know if it's worth using getPluginRemovedDependencies elsewhere? All removed dependencies can be passed to all plugins, I don't see a need for them to be filtered in advance.
Re: suggestion of untangling EntityDisplayBase::onDependencyRemoval in #12, not sure how to go about that. I don't know if it makes sense for a plugin to manage and remove dependent config, because each one isn't responsible for it's own isolated tree of config that it can deal with. A plugin can't move itself from 'content' to 'hidden'. Keen to here if @tim.plunkett has any thoughts on how to approach this.
NR for random fail.
Comment #17
claudiu.cristeaOn order to keep the clarity, I think we should not mix here any use-case.
Comment #18
claudiu.cristeaComment #19
claudiu.cristeaEDIT: Moved the comment to #21
Comment #21
claudiu.cristeaSorry, the patch from #19 was malformed. Interdiff to #13.
This is only a PoC.
Comment #23
sam152 commentedIs there a concrete use case for onUnresolvedPluginDependencyRemoval? It seems like something unique to entity displays, based on their ability to disable fields.
Also, I think it makes sense to actually use the interface outside of a test when it's introduced. The deletions show an example of what the interface gives you free.
Comment #24
claudiu.cristeaThe new methods should return if the change was done or not. The tests are still not fixed.
If a plugin is not able to fix the removed dependencies, the host entity will be removed. In a lot of cases we want to avoid this. This provides a generic way to do something else than let the entity die. For example, some entities will want to remove the plugin from collection, others will decide to replace the plugin with a fallback plugin or will decide to disable themselves (like the entity display does).
Comment #25
claudiu.cristeaComment #27
claudiu.cristeaAgain wrong patch. Sorry!
Comment #29
claudiu.cristeaHere's the test fix.
Comment #30
claudiu.cristeaFixing test. Reverting PluginSettingsBase for now.
Comment #32
tim.plunkett#2830581: Fix ContentModeration workflow type to calculate correct dependencies is now in, so we should address that introduction of onDependencyRemoval() as well.
Comment #33
claudiu.cristeaHere's a try with ImageStyle and the effects plugin collection. But It needs a nice Kernel test.
Modified the EntityWithDependentPluginCollectionInterface::onPluginDependencyRemoval and ::onUnresolvedPluginDependencyRemoval signatures to take also the list of removed dependencies as argument.
Comment #34
claudiu.cristea@tim.plunkett, @Sam152, I think right now we need a partial (more architectural) review about the direction. Then we need to refresh the IS based on the conclusions.
Comment #36
claudiu.cristeaDammit!
Comment #38
claudiu.cristeahuh?
Comment #40
claudiu.cristeaComment #41
tim.plunkettThis is handled in ConfigEntityBase::preSave(), do we really need it again here?
Comment #42
claudiu.cristeaI missed that, thank you. I don't know :)
Comment #43
tim.plunkettI think we should switch to a TDD approach here, it will help avoid adding extra code. Let's define what the goals are, write test coverage for it, and then adjust the internals as needed.
Comment #44
claudiu.cristea@tim.plunkett, here's a proposal:
Comment #45
sam152 commentedThis all looks pretty good, but I think the onPluginDependencyRemoval method needs to be removed from the picture. I don't think it does anything in the example in the patch and I don't see why anything it could conceivably do couldn't be part of the method for the individual plugin. The "let the config entity react" comment isn't really needed if the point was to refresh the config entity, as @tim.plunkett suggested that's the job of the collection.
NW for that + @todos.
Wouldn't this already happen because the image style fails to remove the module as a dependency and onUnresolvedPluginDependencyRemoval removes it?
Comment #46
claudiu.cristeaTrying to work on this weekend.
Comment #47
sam152 commentedHi claudiu.cristea, are you still actively working on this?
Comment #48
claudiu.cristeaComment #49
sam152 commentedI've tried to jump start this up again by rerolling and implementing some of #45.
I don't really understand the test cases or the fails, mocking them for specific return values on a specific number of calls is hard to follow but will find some time to have a look at it all again.
Comment #50
sam152 commentedComment #51
sam152 commentedI think we also need to check the BC policy, I think interface additions when there is a suitable base class are allowed.
Comment #52
claudiu.cristeaThis is wrong for the reasons you can find by analysing and understanding how
EntityDisplayBase::onDependencyRemoval()works. Actually, that is a particular case that we need to expand in a general case here. Also, I've explained that in #44.2 and 3.EDIT: The test
\Drupal\Tests\field_ui\Kernel\EntityDisplayTest::testComponentDependencies(), that I wrote when fixingEntityDisplayBase, is a case that reflect what we need to solve here. There's also a relevant comment in\Drupal\field_test\Plugin\Field\FieldWidget\TestFieldWidget::onDependencyRemoval().EDIT2: So, there are 2 kind of reactions that the entity should be provided. First, when dependency removal are resolved and second, when there are still left unresolved dependencies after the first step.
Comment #53
sam152 commented#44 doesn't have an explanation.
I don't think the pattern of the config entity having specific awareness of the plugins private configuration (and thus changes when the plugin does) is a pattern that should be encouraged by the interfaces, no matter what is in EntityDisplayBase. Generally speaking, the plugin configuration should be self-contained (you know, pluggable...) and there shouldn't typically be much reason for a config entity to change when a plugin cleanly resolves a dependency removal. The plugin config should be encapsulated and irrelevant to the config entity.
Perhaps the patch should have a concrete example of where this method makes sense instead of shoehorning it into ImageStyle. You could remove that whole method from that entity and it would behave the same.
Maybe step 7 should be resolved before core APIs are written to cater for it:
Comment #54
claudiu.cristeaDiscussed with @tim.plunkett on IRC and we agreed for the next plan:
EntityDisplayBaseto properly use plugin collections.This IS will be updated accordingly. I will handle these tasks.
Comment #55
sam152 commentedIs there an issue for fixing EntityDisplayBase?
Comment #56
claudiu.cristea@Sam152, will be handled here.
Comment #67
smustgrave commentedWonder if someone could provide an updated issue summary? Been about 5 years so a lot has changed.
Comment #68
andypostI bet it's about to kinda "behavior.detach()" to allow plugins manage destruction for example
Comment #69
andypostit still very handy to have this reaction, it moves forward action plugins at least
It still makes sense as separate interface
Comment #70
ravi.shankar commentedAdded reroll of patch #49 on Drupal 10.1.x. still needs work for comment #69.
Comment #72
wim leersRelated, with 162 followers: #3056633: It is not possible to uninstall a module that provides a filter plugin via config import.
Comment #75
godotislateCreated MR 13171 with a new approach that is mostly based on the MR for #3326447: Deleting a Media view mode should not result in the deletion of an entire text format, combined with some ideas from the patch in #70:
Drupal\Core\Plugin\RemovableDependentPluginInterfacewith methodonCollectionDependencyRemoval()ConfigEntityBase::onDependencyRemoval()that tests if the entity implementsEntityWithPluginCollectionInterface. If so, iterate through collections and if there are plugin that implementRemovableDependentPluginInterfacedependencies::onCollectionDependencyRemoval(), invoke the method on that plugin so it can react by changing its configuration or removing itself from the collectiononCollectionDependencyRemoval()returns TRUE, thenConfigEntityBase::onDependencyRemoval()will return TRUE, so the config entity's dependencies can be recalculatedThis approach kind of comes at the problem from the other way of the patch. The handling of the dependency removal is done completely in the plugin class, other than the entity invoking the plugin's onCollectionDependencyRemoval() method. The plugin collection and index ID of the plugin instance in the collection are provided as arguments along with the dependencies, so that the plugin instance can be removed from the collection as needed. This approach is kind of based on how
Drupal\views\Entity\View\onDependencyRemoval() invokesDependentWithRemovalPluginInterface::onDependencyRemoval()for handler plugins, as well asFieldItemInterface::onDependencyRemoval(),which has the field definition object as well as the dependencies passed in.There may need to be tests added to test the dependency removal handling generically. The tests in the MR are a kernel test and functional test specific to the the image style and media view mode use cases. The unit tests in #70 no longer apply to the solution.
Also bumping this to critical, because #3326447: Deleting a Media view mode should not result in the deletion of an entire text format combined with #3537624: Deleting filter formats may result in data loss can lead to content data loss. There is a case of this actually happening brought up in slack: https://drupal.slack.com/archives/C1BB308HH/p1755719629686569
Also, with this #3326447: Deleting a Media view mode should not result in the deletion of an entire text format is effectively a duplicate, so it perhaps can be closed with credits transferred here.
Comment #76
godotislateChanged the functional test for media to a kernel test, and added a unit test for more generic coverage.
Comment #77
needs-review-queue-bot commentedThe Needs Review Queue Bot tested this issue. It no longer applies to Drupal core. Therefore, this issue status is now "Needs work".
This does not mean that the patch necessarily needs to be re-rolled or the MR rebased. Read the Issue Summary, the issue tags and the latest discussion here to determine what needs to be done.
Consult the Drupal Contributor Guide to find step-by-step guides for working with issues.
Comment #78
godotislateRebased. Also tagging for Needs CR to document new interface if we go that way.
Comment #79
godotislateComment #80
alexpottI like the new approach from #75. This is looking good. I left one question on the MR. I also like that we've got two use-cases in core addressed in the MR and there are likely to be more in core and contrib.
Comment #81
godotislateAddressed MR comments. Also added the CR: https://www.drupal.org/node/3549101
Comment #82
godotislateComment #83
godotislateComment #84
godotislateComment #85
godotislateComment #86
nicxvan commentedTest coverage looks good. The only thing I didn't like at first was the name, but it follows the established patterns set so not much to suggest beyond that?
The default setting gave me pause because cleaning up new dependency chains can be complex, but contrib can calculate those. I think the only open question is what happens if a method doesn't properly reset dependencies. I assume just what happens now which is it will remove everything. I don't think it's worth holding up over that since it's a much better situation and fixes a data loss bug.
Read through the CR and it seems clear too.
I also read through the IS and it seems up to date and the history, but focused more on the newer comments since the change in direction.
Comment #87
alexpottI've added an API change to consider before we merge this.
Comment #88
godotislateComment #89
godotislateMade changes per #87 to the MR and updated IS and CR. This is ready again.
Comment #90
godotislateJust wanted to make sure to note #3326447: Deleting a Media view mode should not result in the deletion of an entire text format is a duplicate at this point and should be closed with credits transferred here.
Comment #91
nicxvan commentedI had a look, I think the feedback has been addressed. Name makes sense, left a comment on the MR about a discussion I had with @godotislate. I think this is ready again!
Comment #93
alexpottCommitted 7c964ee and pushed to 11.x. Thanks!
Given we have new interfaces and an API I've only put this in 11.x so it'll be part of 11.3.0
Comment #95
nicxvan commentedThanks! I took a pass at credit since I don't think it saved properly.
Ok try to look at the related issues soon.
Comment #97
claudiu.cristeaNice that this has been finally fixed. I wonder if #2562107: EntityDisplayBase should react on removal of its components dependencies could be reworked to use this standard approach.