Proposal: Implement a universal way to download files/media with a dedicated route

Created on 4 October 2017, over 7 years ago
Updated 1 April 2025, about 1 month ago

Problem/Motivation

Some background:

Replacing files in-line in Drupal 8 seems nigh impossible without a lot of hacky workarounds. One of the main motivations to replacing files for most people is retaining the original file URL (/sites/default/files/foo/bar/filename.pdf) because they are using this within menu links, textarea fields, or link fields to link to the file.

However this all breaks down if they go back and replace that file later, because the filename will change to something like:

  • /sites/default/files/foo/bar/filename_0.pdf

or

  • /sites/default/files/foo/bar/[CURRENT.YEAR-CURRENT.MONTH]/filename.pdf

If they use date tokens in the file name path. It could be anything.

The burden is then on the content editor to go through all their content and update references to said file so it does not 404. From history with clients, this is one area without a decent answer that we always get bad feedback about.

Proposed resolution

I was able to solve this in the interim with a contrib module that has received decent feedback from users:

https://www.drupal.org/project/media_entity_download

This module looks at the configuration of the media entity at the source field value, which points to the field on the media entity containing the file item. It also provides a new route:

media/{media}/download

This route looks at the media entity bundle config, grabs the field and then fetches the file for that media item and returns a BinaryFileResponse to serve the file to the client. The route also takes an optional delta value for multivalue fields - if you wanted to link to the third file in a field, you would link to media/{media}/download?delta=2.

With this in place, it no longer matters if the files are replaced or renamed anymore, because the route remains consistent. This lessens the burden on editors and makes replacing/updating files in media less of a hassle.

With Media going into core, I believe this would be a small lift to getting that in alongside it and greatly improving the experience of maintaining a consistent URI for a file that users download. The module provides a field formatter (when rendering the file item in a media entity), Views support, and LinkIt support.

Remaining tasks

Explore effort to supporting this around the core support of Media in 8.4.x+

User interface changes

Considerations

Possible performance, user access / permissions, private file setting for Media vs public, Pathauto support

API changes

Data model changes

Feature request
Status

Closed: duplicate

Component

Idea

Created by

🇺🇸United States kevinquillen

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.

Production build 0.71.5 2024