Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
See title. I fixed this with a hook_file_delete. Here's the code I used:
/**
* Implements hook_file_delete
*
* When an image is deleted, remove it from media_gallery nodes first.
*/
function mymodule_file_delete($file) {
$query = new EntityFieldQuery();
$result = $query->entityCondition('entity_type', 'node')
->fieldCondition('media_gallery_media', 'fid', $file->fid, '=')
->execute();
if (isset($result['node'])) {
$nids = array_keys($result['node']);
$nodes = entity_load('node', $nids);
foreach ($nodes as $node) {
media_gallery_remove_item_from_gallery($node, $file);
}
}
}
Is there any reason why media_gallery doesn't ship with something like this?
Comment | File | Size | Author |
---|---|---|---|
#11 | media_gallery-remove_deleted_files-1715422-11.patch | 888 bytes | Moloc |
#2 | media_gallery-remove_deleted_files-1715422-2.patch | 889 bytes | jsagotsky |
Comments
Comment #1
lsolesen CreditAttribution: lsolesen commentedNope. There is no reason. Could you make a patch that adds the functionality to media_gallery, so we can test it properly?
Comment #2
jsagotsky CreditAttribution: jsagotsky commentedHere ya go!
Comment #3
lsolesen CreditAttribution: lsolesen commentedWhen I try to delete a file from admin/content/media, I get the message: "The file tux.png is in use and cannot be deleted.", also when I go to: media/1/delete
I am in favor of this committing patch, but you need to tell me how to test, whether it works correctly, before I can commit it.
Comment #4
jsagotsky CreditAttribution: jsagotsky commentedThat wasn't my experience. I got something to the effect of "The file is in use. Deleting it may break stuff." Deleting it did in fact break stuff. I'll check again when I'm in the office next week.
Comment #5
Jackinloadup CreditAttribution: Jackinloadup commentedThe discrepancy between #3 and #4 delete messages is due to different file_entity module versions. Also #3 could be using Media 1.x.
File_entity modules delete behavior changed in #1422296: Allow a file to be deleted forcefully in delete callbacks which I believe first appeared in 7.x-2.0-unstable4.
Comment #6
lsolesen CreditAttribution: lsolesen commented@Jackinloadup I just tested with media 1.x, and the message is: "The file 09 (1).jpg is in use and cannot be deleted."
I did the following:
Currently there is no support in Media Gallery for the 2.x branch of media.
Comment #7
jsagotsky CreditAttribution: jsagotsky commented> Currently there is no support in Media Gallery for the 2.x branch of media.
Ah. That explains that. We're using 2.x with a number of patches from the issue queue. I'm happy to ignore this issue for now until MG actually supports 2.x.
Comment #8
lsolesen CreditAttribution: lsolesen commentedComment #9
Moloc CreditAttribution: Moloc commentedTagging Media 2.x
Comment #10
therobyouknow CreditAttribution: therobyouknow commentedI have been able to use the development version of media gallery to get it working with media 2.x - credit to those who worked on media gallery (rather than myself). I have documented my steps to get it working here - comment 102: http://drupal.org/node/1244204#comment-6678836
Comment #11
Moloc CreditAttribution: Moloc commentedThank you for the patch.
Updated patch to use the new file field.
Comment #12
Moloc CreditAttribution: Moloc commentedPatch committed.