Default images for the Image field are not recording file usage properly

Created on 19 November 2024, 8 months ago

Problem/Motivation

There are 2 issues.
First, the image module hooks are adding file usage with \Drupal::service('file.usage')->add($file, 'image', 'default_image', $field_storage->uuid())
But file.usage expects 3rd parameter to be the entity type, not the arbitrary string, and the 4th parameter should be ID, not the UUID. Because of that visiting /admin/content/files/usage/{file_id} throws a 500.

Second, it was spotted while investigating the first issue. File usage records are not added at all if you add the default image while creating field (not updating existing)

Steps to reproduce

Add field type Image to any entity type.
Add default image (in storage or field config)
If the field is new, file usage is not added and the file is temporary
If updating, file usage is added, but with wrong parameters, so /admin/content/files/usage/{file_id} throws a 500 while trying to load the "default_image" entity type.

Proposed resolution

Add file usage records on field creation.
Change "default_image" and uuid used for file.usage table to entity type machine name (field_config and field_storage_config) and entity ID

Remaining tasks

-

User interface changes

-

Introduced terminology

-

API changes

-

Data model changes

-

Release notes snippet

🐛 Bug report
Status

Needs work

Version

11.0 🔥

Component

image system

Created by

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Merge Requests

Comments & Activities

  • Issue created by @reinfate
  • Pipeline finished with Failed
    8 months ago
    Total: 96s
    #343600
  • Does this duplicate one of the issues related to 🌱 Dealing with unexpected file deletion due to incorrect file usage Active or is this a newly-identified bug?

  • Checked the issue summary in https://www.drupal.org/project/drupal/issues/2821423 🌱 Dealing with unexpected file deletion due to incorrect file usage Active and this seems like a newly-identified bug

  • Pipeline finished with Failed
    8 months ago
    Total: 522s
    #343619
  • Some random tests failed, but I'm pretty sure it's not related to this issue.

  • 🇺🇸United States smustgrave

    Appears to need a rebase.

    If you are another contributor eager to jump in, please allow the original poster @reinfate at least 48 hours to respond to feedback first, so they have the opportunity to finish what they started!

  • Pipeline finished with Success
    2 months ago
    Total: 979s
    #489271
  • Issue was unassigned.
  • Status changed to Needs review 2 months ago
  • Rebased from 11.x
    Setting back to needs review

  • 🇺🇸United States smustgrave

    Question what are the odds this could break contrib or custom code that could still be calling

    \Drupal::service('file.usage')->delete($file_old, 'image', 'default_image', $field_storage->uuid());

  • It will not result in a critical error, but yes, in that case, the usages will not be added or deleted properly.

    I would say it is unlikely that there is a lot of code that works with file usage on behalf of the image module.
    And the only solution that I could think of is to deprecate 'image', 'default_image' as 2nd and 3rd params in the file_usage service.

    The PR also needs an update hook to update existing records. I could add that, once there is a green light for the current solution.

  • 🇺🇸United States smustgrave

    If I had to guess deprecation would be the way to go on the safe side but not 100%

  • 🇮🇳India roshanibhangale

    Hi,

    I have tested this ticket on Drupal 11.x.dev and its working as expcted for me.

    Steps followed

    1. Add field type Image to any entity type.
    2. Add default image (in storage or field config)
    3. If the field is new, file usage is not added and the file is temporary
    4. If updating, file usage is added, but with wrong parameters, so /admin/content/files/usage/{file_id} throws a 500 while trying to load the "default_image" entity type.

    Actual Result

    • After applying the patch successfully, able to see the added image is showing as permanant and usage place.

    Hence moving this ticket to RTBC +1

    Attaching the screenshot for the reference.

Production build 0.71.5 2024