Skip to content

Commit

Permalink
SearchControls: Decode $preserveParamsbefore adding/setting to $red…
Browse files Browse the repository at this point in the history
…irectUrl

- The (set/add)Params() method always encodes the params. This way we can avoid encoding the parameters twice.
  • Loading branch information
sukhwinder33445 authored and nilmerg committed Dec 9, 2024
1 parent e880e10 commit 003c51b
Showing 1 changed file with 26 additions and 5 deletions.
31 changes: 26 additions & 5 deletions src/Compat/SearchControls.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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<string, mixed> 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;
}
}

0 comments on commit 003c51b

Please sign in to comment.