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?

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

lsolesen’s picture

Status: Active » Needs work

Nope. There is no reason. Could you make a patch that adds the functionality to media_gallery, so we can test it properly?

jsagotsky’s picture

lsolesen’s picture

Status: Needs work » Needs review

When 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.

jsagotsky’s picture

That 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.

Jackinloadup’s picture

The 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.

lsolesen’s picture

@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:

drush make http://is.gd/8EKNFT --prepare-install media_dev
drush en media_gallery

Currently there is no support in Media Gallery for the 2.x branch of media.

jsagotsky’s picture

> 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.

lsolesen’s picture

Status: Needs review » Postponed
Moloc’s picture

Issue tags: +Beta9-blocker

Tagging Media 2.x

therobyouknow’s picture

> 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.

I 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

Moloc’s picture

Version: 7.x-1.0-beta8 » 7.x-2.x-dev
Status: Postponed » Needs review
FileSize
888 bytes

Thank you for the patch.

Updated patch to use the new file field.

Moloc’s picture

Status: Needs review » Fixed

Patch committed.

Automatically closed -- issue fixed for 2 weeks with no activity.