From dc260258cde273d23ed3da7df6cfe7c9eb7b48ef Mon Sep 17 00:00:00 2001 From: Sukhwinder Dhillon Date: Mon, 25 Nov 2024 14:27:49 +0100 Subject: [PATCH] SearchControls: Decode `$preserveParams`before adding/setting to $redirectUrl - The (set/add)Params() method always encodes the params. This way we can avoid encoding the parameters twice. --- src/Compat/SearchControls.php | 31 ++++++++++++++++++++++++++----- 1 file changed, 26 insertions(+), 5 deletions(-) diff --git a/src/Compat/SearchControls.php b/src/Compat/SearchControls.php index 70b4ec8e..b08b4f6c 100644 --- a/src/Compat/SearchControls.php +++ b/src/Compat/SearchControls.php @@ -57,14 +57,12 @@ public function createSearchBar(Query $query, ...$params): SearchBar $requestUrl = Url::fromRequest(); $preserveParams = array_pop($params) ?? []; $redirectUrl = array_pop($params); - - $requestUrlClone = $requestUrl->onlyWith($preserveParams); - $paramsToAdd = $requestUrlClone->getParams()->toArray(false); + $paramsToAdd = $this->decodedParamValues($preserveParams, $requestUrl); if ($redirectUrl !== null) { $redirectUrl->addParams($paramsToAdd); } else { - $redirectUrl = $requestUrlClone; + $redirectUrl = $requestUrl->onlyWith($preserveParams); } $filter = QueryString::fromString((string) $this->params) @@ -159,7 +157,7 @@ public function createSearchEditor(Query $query, ...$params): SearchEditor $redirectUrl = array_pop($params); $moduleName = $this->getRequest()->getModuleName(); $controllerName = $this->getRequest()->getControllerName(); - $paramsToAdd = $requestUrl->onlyWith($preserveParams)->getParams()->toArray(false); + $paramsToAdd = $this->decodedParamValues($preserveParams, $requestUrl); if ($redirectUrl !== null) { $redirectUrl->addParams($paramsToAdd); @@ -259,4 +257,27 @@ protected function enrichFilterCondition(Filter\Condition $condition, Query $que $condition->metaData()->set('columnLabel', $label); } } + + /** + * Decode the given param names from the given Url + * + * @internal This is only a helping method to prevent params being encoded multiple times in + * {@see SearchControls::createSearchBar()} and {@see SearchControls::createSearchEditor()} + * and therefore should not be used anywhere else. + * + * @return array decoded key => value pairs + */ + private function decodedParamValues(array $paramNames, Url $url): array + { + $params = []; + foreach ($paramNames as $param) { + $val = $url->getParam($param); + + if ($val !== null) { + $params[$param] = $val; + } + } + + return $params; + } }