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.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

axel.rutz created an issue. See original summary.

geek-merlin’s picture

Title: Bubble up file name to media name » Generate file name in presave to allow others to react
geek-merlin’s picture

Simple patch flying in that implements this.

ptmkenny’s picture

Status: Needs review » Needs work

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