Provide File::createFileUrl() as a replacement for the deprecated File:url() implementation

Created on 6 January 2015, almost 11 years ago
Updated 21 September 2025, 22 days ago

Problem/Motivation

The file entity overrides the url() method to to provide a fake entity URL. This was done to to being able to expose the file URL in hal+json API requests. That implementation no longer relies on that, thanks to #2922487: Follow-up for #2910211: fix all deprecation warnings .

Original issue description:

If a file entity is loaded, a call to link() function ($entity->link()), results in following exception:

Drupal\Core\Entity\Exception\UndefinedLinkTemplateException: No link template "canonical" found for the "file" entity type in Drupal\Core\Entity\Entity->urlInfo() (line 188 of /var/www/html/drupal8/core/lib/Drupal/Core/Entity/Entity.php).

Proposed resolution

Originally, this issue was about the fact that toUrl() did not implement that behavior and initial implementations and earlier patches attempted to replace that, in a dynamic way depending on having a canonical link template (which e.g. the file_entity project adds).

The new suggested proposed solution is to replace that with an explicit API method that also allows easy access to the root-relative file URL and even makes that the default.

An entity can not have a canonical link template, and every caller must support that.

Remaining tasks

User interface changes

API changes

See change record: https://www.drupal.org/node/3019830

$file->createFileUrl() == file_url_transform_relative(file_create_url($file->getFileUri()));

file_create_url($file->getFileUri()) == $file->createFileUrl(FALSE);

Data model changes

🐛 Bug report
Status

Fixed

Version

8.7 ⚰️

Component

file.module

Created by

🇬🇧United Kingdom cs_shadow

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.

No activities found.

Production build 0.71.5 2024