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; + } }