From 83cb0c8a61cce7866e3907345c16f1b5bad002ca Mon Sep 17 00:00:00 2001
From: catch <catch@35733.no-reply.drupal.org>
Date: Mon, 7 Feb 2022 16:12:37 +0000
Subject: [PATCH] Issue #3254727 by Leon Kessler, ranjith_kumar_k_u, cmlara,
 andileco: File links with query parameters no longer work

(cherry picked from commit d60ba4d876b435b2a9a2136bf00e638d07bf669d)
---
 core/modules/file/file.module                 |  2 +-
 .../Formatter/FileEntityFormatterTest.php     | 20 ++++++++++++++++++-
 2 files changed, 20 insertions(+), 2 deletions(-)

diff --git a/core/modules/file/file.module b/core/modules/file/file.module
index 7300dd9c6767..04e849466b28 100644
--- a/core/modules/file/file.module
+++ b/core/modules/file/file.module
@@ -1527,7 +1527,7 @@ function template_preprocess_file_link(&$variables) {
   $variables['attributes']->addClass($classes);
   $variables['file_size'] = format_size($file->getSize() ?? 0);
 
-  $variables['link'] = Link::fromTextAndUrl($link_text, $url->setOptions($options))->toRenderable();
+  $variables['link'] = Link::fromTextAndUrl($link_text, $url->mergeOptions($options))->toRenderable();
 }
 
 /**
diff --git a/core/modules/file/tests/src/Kernel/Formatter/FileEntityFormatterTest.php b/core/modules/file/tests/src/Kernel/Formatter/FileEntityFormatterTest.php
index d48a58089a56..a32838bd650f 100644
--- a/core/modules/file/tests/src/Kernel/Formatter/FileEntityFormatterTest.php
+++ b/core/modules/file/tests/src/Kernel/Formatter/FileEntityFormatterTest.php
@@ -16,7 +16,7 @@ class FileEntityFormatterTest extends KernelTestBase {
   /**
    * {@inheritdoc}
    */
-  protected static $modules = ['file', 'user'];
+  protected static $modules = ['file', 'user', 'file_test'];
 
   /**
    * The files.
@@ -171,4 +171,22 @@ public function testFormatterFileSize() {
     }
   }
 
+  /**
+   * Tests the file_link field formatter using a query string.
+   */
+  public function testFormatterFileLinkWithQueryString() {
+    $file = File::create([
+      'uri' => 'dummy-external-readonly://file-query-string?foo=bar',
+      'filename' => 'file-query-string',
+    ]);
+    $file->save();
+    $file_link = [
+      '#theme' => 'file_link',
+      '#file' => $file,
+    ];
+
+    $output = \Drupal::service('renderer')->renderRoot($file_link);
+    $this->assertStringContainsString($this->fileUrlGenerator->generate('dummy-external-readonly://file-query-string?foo=bar')->toUriString(), $output);
+  }
+
 }
-- 
GitLab