diff --git a/core/modules/file/file.module b/core/modules/file/file.module index 1b55fe0e74..b6d024befb 100644 --- a/core/modules/file/file.module +++ b/core/modules/file/file.module @@ -48,6 +48,18 @@ function file_help($route_name, RouteMatchInterface $route_match) { } } +/** + * File URI callback. + * + * @param \Drupal\file\FileInterface $file + * The file entity. + * @return \Drupal\Core\Url + * A Url instance. + */ +function file_uri(FileInterface $file) { + return Url::fromUri($file->getFileUri()); +} + /** * Loads file entities from the database. * diff --git a/core/modules/file/src/Entity/File.php b/core/modules/file/src/Entity/File.php index e35ff2ec91..51da0ba0be 100644 --- a/core/modules/file/src/Entity/File.php +++ b/core/modules/file/src/Entity/File.php @@ -31,7 +31,8 @@ * "label" = "filename", * "langcode" = "langcode", * "uuid" = "uuid" - * } + * }, + * uri_callback = "file_uri" * ) */ class File extends ContentEntityBase implements FileInterface { diff --git a/core/modules/file/tests/src/Unit/FileUrlTest.php b/core/modules/file/tests/src/Unit/FileUrlTest.php index ec0f22ce6f..b89109d493 100644 --- a/core/modules/file/tests/src/Unit/FileUrlTest.php +++ b/core/modules/file/tests/src/Unit/FileUrlTest.php @@ -5,6 +5,7 @@ use Drupal\Core\Entity\EntityInterface; use Drupal\Core\Field\FieldItemInterface; use Drupal\Core\TypedData\DataDefinitionInterface; +use Drupal\Core\Url; use Drupal\file\FileUrl; use Drupal\Tests\UnitTestCase; @@ -26,10 +27,16 @@ class FileUrlTest extends UnitTestCase { * @covers ::getValue */ public function testGetValue() { - $entity = $this->prophesize(EntityInterface::class); - $entity->url() + $url = $this->prophesize(Url::class); + $url->setAbsolute() + ->willReturn($url->reveal()); + $url->toString() ->willReturn($this->testUrl); + $entity = $this->prophesize(EntityInterface::class); + $entity->toUrl() + ->willReturn($url->reveal()); + $parent = $this->prophesize(FieldItemInterface::class); $parent->getEntity() ->shouldBeCalledTimes(1)