Skip to content

Commit

Permalink
Merge pull request #2469 from bolt/better-parameter-override
Browse files Browse the repository at this point in the history
Improve parameter override for Listing Pages
  • Loading branch information
I-Valchev authored Mar 17, 2021
2 parents 24aacd3 + 55cab23 commit 0753739
Showing 1 changed file with 21 additions and 10 deletions.
31 changes: 21 additions & 10 deletions src/Controller/Frontend/ListingController.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,7 @@ public function listing(ContentRepository $contentRepository, string $contentTyp

$page = (int) $this->getFromRequest('page', '1');
$amountPerPage = $contentType->get('listing_records');
$order = $this->getFromRequest('order', $contentType->get('order'));
$queryParams = $this->parseQueryParams($this->request, $contentType);

$params = array_merge($queryParams, [
'status' => 'published',
'order' => $order,
]);
$params = $this->parseQueryParams($this->request, $contentType);

/** @var Content|Pagerfanta $content */
$content = $this->query->getContent($contentTypeSlug, $params);
Expand Down Expand Up @@ -85,14 +79,22 @@ public function listing(ContentRepository $contentRepository, string $contentTyp
private function parseQueryParams(Request $request, ContentType $contentType): array
{
if ($this->config->get('general/query_search') === false) {
return [];
return [
'order' => $contentType->get('order'),
'status' => 'published',
];
}

$queryParams = collect($request->query->all());

$allowedParams = array_merge($contentType['fields']->keys()->all(), $contentType['taxonomy']->all(), ['order']);
// Note, we're not including 'limit', 'printquery', 'returnsingle' or 'returnmultiple' on purpose
$allowedParams = array_merge(
$contentType['fields']->keys()->all(),
$contentType['taxonomy']->all(),
['order', 'earliest', 'latest', 'offset', 'page', 'random', 'author']
);

return $queryParams->mapWithKeys(function ($value, $key) use ($allowedParams) {
$params = $queryParams->mapWithKeys(function ($value, $key) use ($allowedParams) {
// Ensure we don't have arrays, if we get something like `title[]=…` passed in.
if (is_array($value)) {
$value = current($value);
Expand All @@ -105,6 +107,15 @@ private function parseQueryParams(Request $request, ContentType $contentType): a

return in_array($key, $allowedParams, true) ? [$key => $value] : [];
})->toArray();

if (! array_key_exists('order', $params)) {
$params['order'] = $contentType->get('order');
}

// Ensure we only list things that are 'published'
$params['status'] = 'published';

return $params;
}

private function setRecords($content, int $amountPerPage, int $page): Pagerfanta
Expand Down

0 comments on commit 0753739

Please sign in to comment.