Problem/Motivation
If you delete a file that with a usage count > 0, the referring entities are deleted, instead of the references to them.
The message displayed is 'The reference from entity type %ref_type for file %file_name has been deleted.' which implies that the referring entity itself should not be deleted, only the reference to it.
Relevant code from FileDeleteForm.php:
// Remove existing usage for the file.
if (isset($usages['file'])) {
foreach ($usages['file'] as $type => $entities) {
foreach ($entities as $id => $usage_count) {
$ref_entity = $this->entityTypeManager->getStorage($type)->load($id);
if (!empty($ref_entity)) {
$ref_entity->delete();
$this->messenger()
->addMessage($this->t('The reference from entity type %ref_type for file %file_name has been deleted.', [
'%ref_type' => $ref_entity->getEntityType()->id(),
'%file_name' => $this->entity->getFilename(),
]));
}
}
}
}
Setting to critical as involves unexpected deletion of content.
Steps to replicate:
1. Add an image field to node entity
2. Create node "Test node" with image field populated
3. Visit /admin/content/files and click delete for the image added to "Test node"
4. Check both boxes for "Do you want to delete the file immediately?" and "Do you want to force this file to be deleted?"
5. Click "Delete file"
Expected:
Reference to "Test node" is removed
Actual:
The entire entity "Test node" is removed
| Comment | File | Size | Author |
|---|---|---|---|
| #3 | file_delete-force-delete-clear-references-drupalorg.patch | 16.99 KB | cobblestone.consulting |
Issue fork file_delete-3576737
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:
Comments
Comment #2
nicrodgersUpdated the IS and version.
Comment #3
cobblestone.consulting commentedI also ran into this issue and have created a patch that does the following:
Replaces the parent entity delete with a a new Event that gets triggered: FileDeleteEvents:FILE_PRE_FORCE_DELETE. This allows modules that have added usage records to subscribe and add handling for cleaning up the references. The patch includes 2 event subscribers for starters, one for File and one for Media. The Media subscriber runs first and clears any references to the media (from entity references in Fieldable entities) and then deletes the media item. The file subscriber similarly clears any references to the file (except from media) from entity references in Fieldable entities and then removes the usage record (both media and non-media).
At the moment, this still leaves open the possibility (likelihood?) that there will be orphaned references. I think this is a risk that the user is accepting if they are using this functionality in the first place, but it would still be nice to minimize this risk by creating additional Event Subscribers that handle usage references from other modules. "editor" comes to mind as the first one.
Comment #7
smustgrave commentedGoing through my projects for D12 and came across this. I'm also updating my settings so I get notified for new issues so I see this faster. Anyway pushed a fix with test coverage. Claude was used to help write the test. Planning a release soon.