summaryrefslogtreecommitdiffstatshomepage
diff options
context:
space:
mode:
authorcatch <6915-catch@users.noreply.drupalcode.org>2025-03-05 12:33:24 +0000
committercatch <6915-catch@users.noreply.drupalcode.org>2025-03-05 12:33:24 +0000
commitc742c88182271fd58636dc80c652aa958ced82f0 (patch)
tree0e5ebb1abbcde053a2cb937eb69411a1f48182e8
parent9444e3830f9add0e8541f08d8712e98215b76509 (diff)
downloaddrupal-c742c88182271fd58636dc80c652aa958ced82f0.tar.gz
drupal-c742c88182271fd58636dc80c652aa958ced82f0.zip
Issue #3494126 by ramprassad, karimb, nicxvan, kim.pepper, smustgrave, berdir: Move file_get_content_headers() to a method on the file entity
-rw-r--r--core/modules/editor/src/Hook/EditorHooks.php2
-rw-r--r--core/modules/file/file.module10
-rw-r--r--core/modules/file/src/Entity/File.php11
-rw-r--r--core/modules/file/src/FileInterface.php9
-rw-r--r--core/modules/file/src/Hook/FileDownloadHook.php2
-rw-r--r--core/modules/file/tests/file_test/src/Hook/FileTestHooks.php2
6 files changed, 28 insertions, 8 deletions
diff --git a/core/modules/editor/src/Hook/EditorHooks.php b/core/modules/editor/src/Hook/EditorHooks.php
index 952dbff1db5e..e58f922d2ae7 100644
--- a/core/modules/editor/src/Hook/EditorHooks.php
+++ b/core/modules/editor/src/Hook/EditorHooks.php
@@ -319,7 +319,7 @@ class EditorHooks {
}
}
// Access is granted.
- $headers = file_get_content_headers($file);
+ $headers = $file->getDownloadHeaders();
return $headers;
}
diff --git a/core/modules/file/file.module b/core/modules/file/file.module
index 9c10140fa4ba..120d553c5c6d 100644
--- a/core/modules/file/file.module
+++ b/core/modules/file/file.module
@@ -35,13 +35,13 @@ use Drupal\file\Upload\FormUploadedFile;
* @return array
* An associative array of headers, as expected by
* \Symfony\Component\HttpFoundation\StreamedResponse.
+ *
+ * @deprecated in drupal:11.2.0 and is removed from drupal:12.0.0. Use Drupal\file\Entity\FileInterface::getDownloadHeaders() instead.
+ * @see https://www.drupal.org/node/3494172
*/
function file_get_content_headers(FileInterface $file) {
- return [
- 'Content-Type' => $file->getMimeType(),
- 'Content-Length' => $file->getSize(),
- 'Cache-Control' => 'private',
- ];
+ @trigger_error(__FUNCTION__ . '() is deprecated in drupal:11.2.0 and is removed from drupal:12.0.0. Use Drupal\file\Entity\FileInterface::getDownloadHeaders() instead. See https://www.drupal.org/node/3494172', E_USER_DEPRECATED);
+ return $file->getDownloadHeaders();
}
/**
diff --git a/core/modules/file/src/Entity/File.php b/core/modules/file/src/Entity/File.php
index 0022fec6add1..bc310eeb7f22 100644
--- a/core/modules/file/src/Entity/File.php
+++ b/core/modules/file/src/Entity/File.php
@@ -300,4 +300,15 @@ class File extends ContentEntityBase implements FileInterface {
Cache::invalidateTags($tags);
}
+ /**
+ * {@inheritdoc}
+ */
+ public function getDownloadHeaders(): array {
+ return [
+ 'Content-Type' => $this->getMimeType(),
+ 'Content-Length' => $this->getSize(),
+ 'Cache-Control' => 'private',
+ ];
+ }
+
}
diff --git a/core/modules/file/src/FileInterface.php b/core/modules/file/src/FileInterface.php
index 588009cb475b..efa6627790fe 100644
--- a/core/modules/file/src/FileInterface.php
+++ b/core/modules/file/src/FileInterface.php
@@ -142,4 +142,13 @@ interface FileInterface extends ContentEntityInterface, EntityChangedInterface,
*/
public function getCreatedTime();
+ /**
+ * Examines a file entity and returns content headers for download.
+ *
+ * @return array
+ * An associative array of headers, as expected by
+ * \Symfony\Component\HttpFoundation\StreamedResponse.
+ */
+ public function getDownloadHeaders(): array;
+
}
diff --git a/core/modules/file/src/Hook/FileDownloadHook.php b/core/modules/file/src/Hook/FileDownloadHook.php
index 21086c5eb114..1dee2310d57f 100644
--- a/core/modules/file/src/Hook/FileDownloadHook.php
+++ b/core/modules/file/src/Hook/FileDownloadHook.php
@@ -58,7 +58,7 @@ class FileDownloadHook {
return -1;
}
// Access is granted.
- return file_get_content_headers($file);
+ return $file->getDownloadHeaders();
}
}
diff --git a/core/modules/file/tests/file_test/src/Hook/FileTestHooks.php b/core/modules/file/tests/file_test/src/Hook/FileTestHooks.php
index 1f274d9ed0ab..267e2ada834c 100644
--- a/core/modules/file/tests/file_test/src/Hook/FileTestHooks.php
+++ b/core/modules/file/tests/file_test/src/Hook/FileTestHooks.php
@@ -38,7 +38,7 @@ class FileTestHooks {
if (\Drupal::state()->get('file_test.allow_all', FALSE)) {
$files = \Drupal::entityTypeManager()->getStorage('file')->loadByProperties(['uri' => $uri]);
$file = reset($files);
- return file_get_content_headers($file);
+ return $file->getDownloadHeaders();
}
FileTestHelper::logCall('download', [$uri]);
return $this->getReturn('download');