Skip to content

Commit dc26025

Browse files
SearchControls: Decode $preserveParamsbefore adding/setting to $redirectUrl
- The (set/add)Params() method always encodes the params. This way we can avoid encoding the parameters twice.
1 parent e880e10 commit dc26025

File tree

1 file changed

+26
-5
lines changed

1 file changed

+26
-5
lines changed

src/Compat/SearchControls.php

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -57,14 +57,12 @@ public function createSearchBar(Query $query, ...$params): SearchBar
5757
$requestUrl = Url::fromRequest();
5858
$preserveParams = array_pop($params) ?? [];
5959
$redirectUrl = array_pop($params);
60-
61-
$requestUrlClone = $requestUrl->onlyWith($preserveParams);
62-
$paramsToAdd = $requestUrlClone->getParams()->toArray(false);
60+
$paramsToAdd = $this->decodedParamValues($preserveParams, $requestUrl);
6361

6462
if ($redirectUrl !== null) {
6563
$redirectUrl->addParams($paramsToAdd);
6664
} else {
67-
$redirectUrl = $requestUrlClone;
65+
$redirectUrl = $requestUrl->onlyWith($preserveParams);
6866
}
6967

7068
$filter = QueryString::fromString((string) $this->params)
@@ -159,7 +157,7 @@ public function createSearchEditor(Query $query, ...$params): SearchEditor
159157
$redirectUrl = array_pop($params);
160158
$moduleName = $this->getRequest()->getModuleName();
161159
$controllerName = $this->getRequest()->getControllerName();
162-
$paramsToAdd = $requestUrl->onlyWith($preserveParams)->getParams()->toArray(false);
160+
$paramsToAdd = $this->decodedParamValues($preserveParams, $requestUrl);
163161

164162
if ($redirectUrl !== null) {
165163
$redirectUrl->addParams($paramsToAdd);
@@ -259,4 +257,27 @@ protected function enrichFilterCondition(Filter\Condition $condition, Query $que
259257
$condition->metaData()->set('columnLabel', $label);
260258
}
261259
}
260+
261+
/**
262+
* Decode the given param names from the given Url
263+
*
264+
* @internal This is only a helping method to prevent params being encoded multiple times in
265+
* {@see SearchControls::createSearchBar()} and {@see SearchControls::createSearchEditor()}
266+
* and therefore should not be used anywhere else.
267+
*
268+
* @return array<string, mixed> decoded key => value pairs
269+
*/
270+
private function decodedParamValues(array $paramNames, Url $url): array
271+
{
272+
$params = [];
273+
foreach ($paramNames as $param) {
274+
$val = $url->getParam($param);
275+
276+
if ($val !== null) {
277+
$params[$param] = $val;
278+
}
279+
}
280+
281+
return $params;
282+
}
262283
}

0 commit comments

Comments
 (0)