diff --git a/core/modules/file/file.link_relation_types.yml b/core/modules/file/file.link_relation_types.yml new file mode 100644 index 0000000..e3c9c4e --- /dev/null +++ b/core/modules/file/file.link_relation_types.yml @@ -0,0 +1,3 @@ +download: + uri: https://drupal.org/link-relations/download + description: A URL where the file can be downloaded direclty. diff --git a/core/modules/file/src/Entity/File.php b/core/modules/file/src/Entity/File.php index cd7a658..644b035 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; @@ -30,6 +31,9 @@ * "label" = "filename", * "langcode" = "langcode", * "uuid" = "uuid" + * }, + * links = { + * "download" = "/fake-pattern" * } * ) */ @@ -47,6 +51,19 @@ public function getFilename() { /** * {@inheritdoc} */ + public function toUrl($rel = 'canonical', array $options = []) { + if ($rel === 'download') { + $file_url = file_create_url($this->getFileUri()); + $relative = file_url_transform_relative($file_url); + $base = 'base:' . str_replace(base_path(), '', $relative); + return Url::fromUri($base); + } + return parent::toUrl($rel, $options); + } + + /** + * {@inheritdoc} + */ public function setFilename($filename) { $this->get('filename')->value = $filename; }