From 81b3c7763c135c0de412dec6ffb3977083d11f9e Mon Sep 17 00:00:00 2001
From: David Cameron <david@cadeyrn.us>
Date: Tue, 4 Mar 2025 18:55:11 -0600
Subject: [PATCH 1/2] UrlHelper::buildQuery() adds extra ampersands

---
 core/lib/Drupal/Component/Utility/UrlHelper.php          | 9 +++++++--
 .../Drupal/Tests/Component/Utility/UrlHelperTest.php     | 3 ++-
 2 files changed, 9 insertions(+), 3 deletions(-)

diff --git a/core/lib/Drupal/Component/Utility/UrlHelper.php b/core/lib/Drupal/Component/Utility/UrlHelper.php
index 3b32dc9625e6..1d28a55fb5e7 100644
--- a/core/lib/Drupal/Component/Utility/UrlHelper.php
+++ b/core/lib/Drupal/Component/Utility/UrlHelper.php
@@ -45,11 +45,16 @@ public static function buildQuery(array $query, $parent = '') {
     foreach ($query as $key => $value) {
       $key = ($parent ? $parent . rawurlencode('[' . $key . ']') : rawurlencode($key));
 
+      if ($value === NULL) {
+        continue;
+      }
       // Recurse into children.
       if (is_array($value)) {
-        $params[] = static::buildQuery($value, $key);
+        if ($children_query = static::buildQuery($value, $key)) {
+          $params[] = $children_query;
+        }
       }
-      // If a query parameter value is NULL, only append its key.
+      // If a query parameter value is not set, only append its key.
       elseif (!isset($value)) {
         $params[] = $key;
       }
diff --git a/core/tests/Drupal/Tests/Component/Utility/UrlHelperTest.php b/core/tests/Drupal/Tests/Component/Utility/UrlHelperTest.php
index 158e626c8213..9f96cf12f307 100644
--- a/core/tests/Drupal/Tests/Component/Utility/UrlHelperTest.php
+++ b/core/tests/Drupal/Tests/Component/Utility/UrlHelperTest.php
@@ -29,7 +29,8 @@ public static function providerTestBuildQuery() {
       [[' &#//+%20@۞' => 'a'], '%20%26%23%2F%2F%2B%2520%40%DB%9E=a', 'Key was properly encoded.'],
       [['a' => '1', 'b' => '2', 'c' => '3'], 'a=1&b=2&c=3', 'Multiple values were properly concatenated.'],
       [['a' => ['b' => '2', 'c' => '3'], 'd' => 'foo'], 'a%5Bb%5D=2&a%5Bc%5D=3&d=foo', 'Nested array was properly encoded.'],
-      [['foo' => NULL], 'foo', 'Simple parameters are properly added.'],
+      [['foo' => NULL], '', 'Simple parameters are properly added.'],
+      [['a' => '1', 'b' => [], 'c' => []], 'a=1', 'Empty key ignored.'],
     ];
   }
 
-- 
GitLab


From 9617a333212516a0d0fa6267bfa4595353636fde Mon Sep 17 00:00:00 2001
From: David Cameron <david@cadeyrn.us>
Date: Tue, 4 Mar 2025 20:04:12 -0600
Subject: [PATCH 2/2] Removed the redundant condition marked by phpstan

---
 core/lib/Drupal/Component/Utility/UrlHelper.php | 4 ----
 1 file changed, 4 deletions(-)

diff --git a/core/lib/Drupal/Component/Utility/UrlHelper.php b/core/lib/Drupal/Component/Utility/UrlHelper.php
index 1d28a55fb5e7..9a5e76ba735b 100644
--- a/core/lib/Drupal/Component/Utility/UrlHelper.php
+++ b/core/lib/Drupal/Component/Utility/UrlHelper.php
@@ -54,10 +54,6 @@ public static function buildQuery(array $query, $parent = '') {
           $params[] = $children_query;
         }
       }
-      // If a query parameter value is not set, only append its key.
-      elseif (!isset($value)) {
-        $params[] = $key;
-      }
       else {
         // For better readability of paths in query strings, we decode slashes.
         $params[] = $key . '=' . str_replace('%2F', '/', rawurlencode($value));
-- 
GitLab