Problem/Motivation
More than once I've seen custom implementations of warning messages when referenced entities are being edited or deleted. Maybe we could add this option directly in Entity Usage, with a way to opt-out in case sites want to do their own messaging.
Proposed resolution
1) Edit warning messages
- Add a new config setting such as "Show warning when referenced entities are being edited", active by default on new installs (but not changing existing sites)
- When selected, whenever an entity is edited, we would show a warning message alerting users that modifications would affect all usages
2) Delete warning messages
- Add a new config setting such as "Show warning when referenced entities are being deleted", active by default on new installs (but not changing existing sites)
- When selected, the delete form (confirmation step) would show a warning alerting users that there are active references to the entity that is about to be deleted.
The messaging itself may depend on how we want to implement things. For instance, it can be as simple as detecting if there are any usages, and show something like:
There are _recorded usages_ of this entity. (...)
Where the "recorded usages" would be linked to the usage list of that entity.
Ideally though, the message could be smarter and provide more meaningful information. For example:
- On the delete form there is plenty of space, maybe it wouldn't be a problem to show titles (with links) of entities that have references in their default revision? If the list is too big it could be trimmed to the first X results...
- Or we could show just a count of sources?
- Or we could even expose the message to be configurable as well? (with some sort of token to link to the usage page, etc)
- Etc.
Thoughts?
Remaining tasks
User interface changes
API changes
Data model changes
| Comment | File | Size | Author |
|---|---|---|---|
| #26 | interdiff-25-26.txt | 1.02 KB | marcoscano |
| #26 | 3002316-26.patch | 26.46 KB | marcoscano |
| #25 | interdiff-3002316-22-25.txt | 3.13 KB | johnchque |
| #25 | 3002316-25.patch | 26.94 KB | johnchque |
| #22 | interdiff-3002316-20-22.txt | 13.95 KB | johnchque |
Comments
Comment #2
miro_dietikerSuch things would be awesome.
1) The need for warnings vary based on the usage type. Some references are true embeds where awareness is key because the information is embedded in the usage origin. A media item description might be shown immediately. Links in content (linkit) are usually no reason to warn on edit.
2) Agree showing the items individually with label is a great thing as long as the list is not too long.
Also here, some fields need to be excluded. You don't want to warn the user that Paragraphs are deleted when the Host entity is deleted - that's obvious.
Comment #3
berdir> Also here, some fields need to be excluded. You don't want to warn the user that Paragraphs are deleted when the Host entity is deleted - that's obvious.
Then don't track them, which they by default aren't :)
Also don't get why it shouldn't warn on links, those kind of embeds/links are just as problematic when being deleted, e.g. they can be embedded download links to files.
Comment #4
miro_dietikerInteresting...
So you think that it's fine to treat them all the same?
Just asking - how about term usage or user (author) usage? Just don't track? At least term usage can be of great value because regular term views only show content.
Maybe some type of references should disallow delete?
(Or maybe we should offer a permission to still delete even if it is used.)
And in case we still allow deletion - we maybe should offer a method to fix broken links. From simply filtering them out when rendering to batch updating the content to remove the reference. What do you think? ;-)
Comment #5
berdirI'm thinking that this isn't about preventing deletion, not for now anyway. It's just a warning, just like we're doing ourself in paragraps_library edit. We already discussed that preventing deletion of used paragraphs_library_item is flawed and doesn't work and needs to be replaced with a warning message.
And yes, users are by default not tracked and we also don't track terms in our sites.
Comment #6
marcoscanoYes, I would definitely want to keep this restricted to only show a warning/error in the edit/delete form.
We can think of the best way to accomplish that so sites also have flexibility to fine-tune it to their needs.
If we really want to go down the deletion prevention rabbit hole I'd imagine we would do that in a separate module/submodule.
Comment #7
marcoscanoFirst shot at this, if the approach seems reasonable we can proceed fine-tuning it and adding tests.
The optional textarea that allows entering a list of classes for the delete message is aimed at leaving the door open to sites to also show the message in other forms as well. For instance, I have a site where we have built a custom
MoveToTrashFormconfirmation form, which will as a result unpublish the entity. It will be nice to get this message for free there too.Edit warning:
Delete warning:
Comment #8
marcoscanoAnd a screenshot of the new configs:
Comment #9
marcoscanoSmall tweak to make it more flexible when using in custom forms.
Comment #10
marcoscanoOops.
Comment #11
berdirJust put that in entity_usage.settings.yml, that is only automatically imported on new installs.
In #3002571: Multiple warning messages when having untranslatable fields, we switched to #theme status_messages, a bit simpler and doesn't require us to hardcode classes.
not very user friendly but I know it's tricky.
We do have the operation though (\Drupal\Core\Entity\EntityForm::getOperation) and we cna also check whether an entity type has usage tracking enabled, that might be enough?
Comment #12
marcoscano@Berdir thanks for reviewing!
#11:
1: Fixed
2: Fixed
3: \Drupal\Core\Entity\EntityForm has a lot bundled... I wanted to leave this flexible enough so it could be used in other forms as well. Maybe I'm making it more complex than it should be, but the idea is that as long as your form has a
::getEntity()method returning an EntityInterface object, it can be used here. I've added some more info in the description, I hope it helps. (I actually have a use case that I want to use this in a form that just extends ConfirmFormBase, not EntityForm... :) )Comment #13
berdir3. I see. Anything that has getEntity() also has an operation, but you can't check for the entity type at the same time. Ok I guess, just seems a bit strange to have that UI :)
Comment #14
marcoscanoMarking NW for the missing tests.
Comment #15
arpad.rozsa commentedWorking on updating the paragraphs_library module to use this and to remove the custom warning messages from there. But if we would enable it automatically, then this would be also enabled on other entities, which users might not want to see everywhere.
So we need to consider to think about adding an option to enable the messages per entity type.
This way other modules would also benefit, if they only want to enable them, for their entity type.
Comment #16
marcoscanoTrue, I can see your point.
The first alternative that comes to my mind is to have another set of checkboxes per message type, that shows up only when you opt-in to that message, where you could select entity types where the message should be shown. Leaving them empty would show the warning in all entity types.
Any drawbacks of this approach?
Comment #17
marcoscanoComment #19
arpad.rozsa commentedThe delete_warning_form_classes setting is not used anymore, so it should be also removed from here.
Here I think 1 foreach should be enough, since the 2 do the same, except for different warning message types. So move the logic from the first into the second foreach and it should be fine.
The dot at the end of the above sentence is not needed, also in 'Entity types to show warning on edit form.'. One reason that the test would fail.
Also you need to login with a user with the 'administer entity usage' permission to be able to change the settings.
Other test are failing because the test base is changed to the WebDriverTestBase. So revision information is not visible by default, we can't get the status code back now and so on.
Even if those tests are failing, the patch works properly. And those fails are not really connected directly to the new functionality introduced in here.
Comment #20
johnchqueAddressing all changes from previous comments. Working on fixing tests now. :)
Comment #22
johnchqueThis WebDriver Tests are awesome and tricky at the same time. Fixing all tests I think. :)
Comment #23
berdirThe form classes stuff is still "used", it was just removed from the UI, so part of the changes in #20 should be reverted.
Comment #24
marcoscanoThanks a lot for working on this! I've been wanting to come back and finish this for several weeks but haven't found the time yet.
This looks great, the only modif necessary is indeed:
As @Berdir mentioned, we decided some comments above to keep the
delete_warning_form_classesconfig behind the scenes, so sites could still use it (for example by setting its value with drush, CMI or programmatically), and just not expose it on the config form (falling back to theContentEntityDeleteFormclass if it's not defined).Also, thanks a lot for fixing the unrelated test failures! :)
Comment #25
johnchqueOhh that's true. Adding that logic back. :)
Comment #26
marcoscanoWonderful, thanks!
Small tweak to remove some obsolete lines (sorry I may have misled you on this with my previous comment), and I think we should be ready to go.
Comment #28
marcoscanoCommitted and pushed to dev.
Thanks everyone!
Comment #29
marcoscanoFor the record, I tagged alpha8 to include this.