While testing for issue #1918762, I noticed that if I created multiple badges and set some to use an image from the library (previously uploaded for another badge), all file usage did not appear in the {file_usage} table. There was only one entry for the use of a given image file, no matter how many badges were using the image.

To replicate:

  • create a badge and upload an image for it
  • create a second badge and choose to use the image already in the library
  • view the {file_usage} table in the database and note that there is only one usage entry for that file (fid in {file_managed})

Proposed resolution:

  • file usage is tracked on a per-badge basis, so that if multiple badges are using the same image, it is tracked in the database
  • file usage is added/removed when badge images are later swapped or edited
  • file usage is removed when a badge is deleted

For the above, need to make certain that we can still see images in the library, even if no badges are using them at the moment. Need to look into how we get images to show on that tab. Grabbing this for myself at the moment, since I opened the can of worms...

CommentFileSizeAuthor
#1 user_badges-file-usage-1969668-0.patch10.32 KBindyana
Support from Acquia helps fund testing for Drupal Acquia logo

Comments

indyana’s picture

Status: Active » Needs review
FileSize
10.32 KB

Finished working on this. It required several changes, including adding detected badge images to the database for tracking and adding/deleting file usage markers for individual badges. A more detailed summary is below. This should more accurately track the usage state of images. Previously, there were situations where an image in use could be deleted.

The patch is for the current 7.x-2.x-dev; I did no testing for other code bases. Please comment with results if you apply this patch.

Changes:

  • When scanning images directory (public://badges), unmanaged image files are added to the {file_managed} table for tracking. This occurs when viewing the Images tab and when creating/editing badges.
  • When images are deleted on the Images tab, they are only deleted if they are managed files and they are not in use by any module. Result messages are now posted on successful delete or error.
  • When badge is created or edited, file usage is added for the new image (upload/library only).
  • When badge is edited, file usage is deleted for the old image (upload/library only).
  • When badge is deleted, file usage is deleted for the image (library only).
shabana.navas’s picture

Thanks for the patch. These changes require more testing by other users. I have fixed a similar issue (https://drupal.org/node/1983978) where images could not be deleted. Now, you can delete an image that is no longer used by any badges and modules. Once other users have okayed this patch, I will go ahead and commit it asap.

shabana.navas’s picture

Status: Needs review » Fixed

I have ported and committed the patch to the 7.x-3.x-dev version. Please test it out and give your feedback. Thanks @indyana for the patch. Great work!

shabana.navas’s picture

Status: Fixed » Needs review
jlandfried’s picture

Status: Needs review » Reviewed & tested by the community

I'm no longer seeing the issue in the 7.x-3.x-dev version, so this looks good to me!

shabana.navas’s picture

Great to hear! It will definitely find its way to the production version soon.

shabana.navas’s picture

Issue summary: View changes
Status: Reviewed & tested by the community » Fixed

Have added this to the production verison 7.x-2.0. Thanks for your work @indyana.

Status: Fixed » Closed (fixed)

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