Problem/Motivation

When this code is executed on a file entity:

$entity->url('canonical')

The url that is returned is an absolute url, on all other entities in Drupal, the url that is returned is a relative url.

This code is in EntityReferenceFieldItemNormalizer::normalize() which produces a mixed response like this:

{
  "uid": [
    {
      "target_id": "0",
      "url": "/user/0"
    }
  ],
  "field_thumbnail": [
    {
      "target_id": "1362526",
      "alt": null,
      "title": null,
      "width": null,
      "height": null,
      "url": "https://example.com/sites/default/files/video/thumbnail/2016/03/22/mco4_gp_0.jpg"
    }
  ]
}

This means that File entities do not match other entity types and produces an inconsistent API. :(

Proposed resolution

I know that with some stream wrappers you cannot have a relative path, but you should be able to always have a relative path with the public and private stream wrapper(s).

The solution should be to modify the stream wrappers to return a relative url. However, since this might be a breaking change, we should simply parse what is returned from the stream wrapper, if the result has the same domain as the current domain of the request, then we can just return the relative path (like all other entities), rather than the absolute url.

Remaining tasks

  1. Write Patch

User interface changes

None.

API changes

File entities will now be consistent with all other entities and return a relative url (when possible).

Data model changes

None.

Comments

davidwbarratt created an issue. See original summary.

Berdir’s picture

The url() override in the file entity class should be dropped.

It's deprecated and the whole rest integration only works because we call a deprecated method.Once we switch that to toUrl(), it will fail.

If we want to have the URL available, we should instead have a special normalizer for files. See also #1927648: Allow creation of file entities from binary data via REST requests.

Only problem is, existing code might now rely on this method, especially in twig templates. So maybe we do have to keep it, and update rest to do things differently instead.

alexpott’s picture

Funny enough the absolute path is really likely to cause issues in the templates. It certainly did for a recent C3 client. For example, if the user is logged in and under https the render cache can get polluted with https links.

alexpott’s picture

Also somewhat amusingly...

File::load(1)->toUrl()

returns...

Drupal\Core\Entity\Exception\UndefinedLinkTemplateException with message 'No link template 'canonical' found for the 'file' entity type'

alexpott’s picture

Version: 8.2.x-dev » 8.3.x-dev

Drupal 8.2.0-beta1 was released on August 3, 2016, which means new developments and disruptive changes should now be targeted against the 8.3.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.3.x-dev » 8.4.x-dev

Drupal 8.3.0-alpha1 will be released the week of January 30, 2017, which means new developments and disruptive changes should now be targeted against the 8.4.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.4.x-dev » 8.5.x-dev

Drupal 8.4.0-alpha1 will be released the week of July 31, 2017, which means new developments and disruptive changes should now be targeted against the 8.5.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.5.x-dev » 8.6.x-dev

Drupal 8.5.0-alpha1 will be released the week of January 17, 2018, which means new developments and disruptive changes should now be targeted against the 8.6.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Wim Leers’s picture

Version: 8.6.x-dev » 8.7.x-dev

Drupal 8.6.0-alpha1 will be released the week of July 16, 2018, which means new developments and disruptive changes should now be targeted against the 8.7.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.7.x-dev » 8.8.x-dev

Drupal 8.7.0-alpha1 will be released the week of March 11, 2019, which means new developments and disruptive changes should now be targeted against the 8.8.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.8.x-dev » 8.9.x-dev

Drupal 8.8.0-alpha1 will be released the week of October 14th, 2019, which means new developments and disruptive changes should now be targeted against the 8.9.x-dev branch. (Any changes to 8.9.x will also be committed to 9.0.x in preparation for Drupal 9’s release, but some changes like significant feature additions will be deferred to 9.1.x.). For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

Version: 8.9.x-dev » 9.1.x-dev

Drupal 8.9.0-beta1 was released on March 20, 2020. 8.9.x is the final, long-term support (LTS) minor release of Drupal 8, which means new developments and disruptive changes should now be targeted against the 9.1.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

Version: 9.1.x-dev » 9.2.x-dev

Drupal 9.1.0-alpha1 will be released the week of October 19, 2020, which means new developments and disruptive changes should now be targeted for the 9.2.x-dev branch. For more information see the Drupal 9 minor version schedule and the Allowed changes during the Drupal 9 release cycle.

Berdir’s picture

Status: Active » Closed (duplicate)

I'm just going to close this.

url() is gone and toUrl() now has a consistent result in Drupal 9 (meaning, an exception, because it has no canonical link template in core), there is a separate method to the the relative or absolute file URL, createFileUrl().