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

Created on 19 November 2024, about 1 month 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

Production build 0.71.5 2024