Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
Problem/Motivation
Files still aren't first class entities although we have a File entity
Proposed resolution
Consider adding a canonical link template for files to core.
Remaining tasks
Do it
User interface changes
None
API changes
None
Comments
Comment #7
John Pitcairn CreditAttribution: John Pitcairn commentedThat would be handy. Redirecting to a file at present present some difficulties.
Comment #8
BerdirWon't fix IMHO, with media.module, files will IMHO remain second-class citizens and we will not introduce an actual canonical route in core, and we decided against having one that points to the physical file.
Comment #9
no74hUsing File entity as an Entity type may lead to unwanted behaviors. One of which I encounter when trying to get url of File instance by calling this: `$file->toUrl()`
Abstract class Entity (which File entity indirectly extends) does implement toUrl() but it's not suitable with File entity.
Because File entity doesn't have default link templates (canonical); therefore this error is raised.
(In my case, overriding toUrl() in File class and returning File::url() can avoid this exception. But it's not the solution for this issue. )
I think, if file is considered an Entity type, it has to respect EntityInterface specs.
Not having canonical routes that points to physical files is legit too, but should Files in this case be of another type than Entities?
Comment #10
BerdirtoUrl() is documented as throwing exceptions if a link template doesn't exist, you have to handle that or check first. Files are entities, nothing says that entities must have canonical link templates.
Closing this now as I suggested above.