Generate file name in presave to allow others to react

Created on 18 May 2017, almost 8 years ago
Updated 12 April 2023, about 2 years ago

Problem/Motivation

Core will make media entity the standard wrapper for files. But file name currently does not bubble up to the media name (so this stays the originally uploaded file name). This is a PITA.

Even worse, there is no voodoo-free fix for this in custom hooks as
* the filefield_paths file name is generated in hook_media_insert/update which is *after* saving
In some cases this can be mitigated, but not if the logic relies on the actual file system name with possible serial suffix which is not known before file save.

Proposed resolution

* Move the filefield_paths logic to a late hook_entity_presave (which is after media field magick, see \Drupal\Core\Entity\EntityStorageBase::doPreSave)
* add an option "Adjust media name" if file field is on a media item

Remaining tasks

Code, test, commit.

User interface changes

Added option.

API changes

None.

Data model changes

None.

📌 Task
Status

Needs work

Version

1.0

Component

Code

Created by

🇩🇪Germany geek-merlin Freiburg, Germany

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

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

  • 🇯🇵Japan ptmkenny

    I attempted to apply this patch because Filefield Paths is causing the generated media names on my site to be the temporary file name (example: _6b4f2e5a-d5d4-49cc-a184-3a2107a64f1a.jpeg) instead of the name generated with filefield paths (cat-waiting-at-the-window.jpeg).

    Unfortunately, the patch no longer applies to the current version of the module so I'm marking as "Needs Work."

Production build 0.71.5 2024