core/modules/file/src/Entity/File.php | 31 ++++++++++++++++++++++ .../EntityResource/Media/MediaResourceTestBase.php | 2 ++ 2 files changed, 33 insertions(+) diff --git a/core/modules/file/src/Entity/File.php b/core/modules/file/src/Entity/File.php index a9ade9e..dee25ab 100644 --- a/core/modules/file/src/Entity/File.php +++ b/core/modules/file/src/Entity/File.php @@ -7,6 +7,7 @@ use Drupal\Core\Entity\EntityStorageInterface; use Drupal\Core\Entity\EntityTypeInterface; use Drupal\Core\Field\BaseFieldDefinition; +use Drupal\Core\Url; use Drupal\file\FileInterface; use Drupal\user\UserInterface; @@ -71,12 +72,42 @@ public function setFileUri($uri) { * @see file_url_transform_relative() */ public function url($rel = 'canonical', $options = []) { + // @todo Remove this work-around before Drupal 9.0.0. This was introduced as + // a temporary work-around in https://www.drupal.org/node/2277705 to provide + // REST support for file entities. https://www.drupal.org/node/2825487 makes + // this obsolete. return file_create_url($this->getFileUri()); } /** * {@inheritdoc} */ + public function toUrl($rel = 'canonical', array $options = []) { + // Lie because we must provide BC. + // @see ::url() + // @todo Remove in https://www.drupal.org/node/2825487. + if ($rel === 'canonical') { + return Url::fromUri($this->url()); + } + return parent::toUrl($rel, $options); + } + + /** + * {@inheritdoc} + */ + public function hasLinkTemplate($rel) { + // Lie because we must provide BC. + // @see ::url() + // @todo Remove in https://www.drupal.org/node/2825487. + if ($rel === 'canonical') { + return TRUE; + } + return parent::hasLinkTemplate($rel); + } + + /** + * {@inheritdoc} + */ public function getMimeType() { return $this->get('filemime')->value; } diff --git a/core/modules/rest/tests/src/Functional/EntityResource/Media/MediaResourceTestBase.php b/core/modules/rest/tests/src/Functional/EntityResource/Media/MediaResourceTestBase.php index 3de224a..475a1ca 100644 --- a/core/modules/rest/tests/src/Functional/EntityResource/Media/MediaResourceTestBase.php +++ b/core/modules/rest/tests/src/Functional/EntityResource/Media/MediaResourceTestBase.php @@ -154,6 +154,7 @@ protected function getExpectedNormalizedEntity() { 'target_id' => (int) $file->id(), 'target_type' => 'file', 'target_uuid' => $file->uuid(), + 'url' => $file->url(), ], ], 'thumbnail' => [ @@ -165,6 +166,7 @@ protected function getExpectedNormalizedEntity() { 'target_type' => 'file', 'target_uuid' => $thumbnail->uuid(), 'title' => 'Llama', + 'url' => $thumbnail->url(), ], ], 'status' => [