diff --git a/config/bolt/config.yaml b/config/bolt/config.yaml index 03c00023b..4e38c7a8f 100644 --- a/config/bolt/config.yaml +++ b/config/bolt/config.yaml @@ -96,8 +96,8 @@ listing_template: listing.twig listing_records: 6 listing_sort: datepublish DESC -# Allow filtering on listing pages using query parameters, much like you -# would with {% setcontent %}. E.g. /pages?order=id and /pages?title=%voluptat% +# Allow filtering on listing pages using query parameters, much like you would +# with {% setcontent %}. E.g. /pages?order=id and /pages?title--like=voluptat # Useful for search. query_search: true diff --git a/ecs.php b/ecs.php index 511e094e6..4de4cdaea 100644 --- a/ecs.php +++ b/ecs.php @@ -40,6 +40,8 @@ use PhpCsFixer\Fixer\Semicolon\NoSinglelineWhitespaceBeforeSemicolonsFixer; use PhpCsFixer\Fixer\Whitespace\NoTrailingWhitespaceFixer; use SlevomatCodingStandard\Sniffs\ControlStructures\DisallowYodaComparisonSniff; +use Symplify\CodingStandard\Fixer\ArrayNotation\ArrayListItemNewlineFixer; +use Symplify\CodingStandard\Fixer\ArrayNotation\ArrayOpenerAndCloserNewlineFixer; use Symplify\CodingStandard\Fixer\ArrayNotation\StandaloneLineInMultilineArrayFixer; use Symplify\CodingStandard\Fixer\Commenting\RemoveSuperfluousDocBlockWhitespaceFixer; use Symplify\CodingStandard\Fixer\Strict\BlankLineAfterStrictTypesFixer; @@ -66,6 +68,8 @@ NativeConstantInvocationFixer::class => null, NativeFunctionInvocationFixer::class => null, UnaryOperatorSpacesFixer::class => null, + ArrayOpenerAndCloserNewlineFixer::class => null, + ArrayListItemNewlineFixer::class => null, ]); $services = $containerConfigurator->services(); diff --git a/src/Controller/Frontend/ListingController.php b/src/Controller/Frontend/ListingController.php index b781bc4a0..7ce577014 100644 --- a/src/Controller/Frontend/ListingController.php +++ b/src/Controller/Frontend/ListingController.php @@ -10,6 +10,7 @@ use Bolt\Entity\Content; use Bolt\Repository\ContentRepository; use Bolt\Storage\Query; +use Pagerfanta\Adapter\ArrayAdapter; use Pagerfanta\Pagerfanta; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; @@ -61,8 +62,7 @@ public function listing(ContentRepository $contentRepository, string $contentTyp return $this->forward($controller, ['slugOrId' => $content->getId()]); } - $records = $content->setMaxPerPage($amountPerPage) - ->setCurrentPage($page); + $records = $this->setRecords($content, $amountPerPage, $page); $templates = $this->templateChooser->forListing($contentType); $this->twig->addGlobal('records', $records); @@ -85,6 +85,11 @@ private function parseQueryParams(Request $request): array $queryParams = collect($request->query->all()); return $queryParams->mapWithKeys(function ($value, $key) { + // Ensure we don't have arrays, if we get something like `title[]=…` passed in. + if (is_array($value)) { + $value = current($value); + } + if (str::endsWith($key, '--like')) { $key = str::removeLast($key, '--like'); $value = '%' . $value . '%'; @@ -93,4 +98,16 @@ private function parseQueryParams(Request $request): array return [$key => $value]; })->toArray(); } + + private function setRecords($content, int $amountPerPage, int $page): Pagerfanta + { + if ($content instanceof Pagerfanta) { + $records = $content->setMaxPerPage($amountPerPage) + ->setCurrentPage($page); + } else { + $records = new Pagerfanta(new ArrayAdapter([])); + } + + return $records; + } } diff --git a/src/Storage/SelectQuery.php b/src/Storage/SelectQuery.php index dd5ab67fb..9a71431d4 100644 --- a/src/Storage/SelectQuery.php +++ b/src/Storage/SelectQuery.php @@ -4,6 +4,7 @@ namespace Bolt\Storage; +use Bolt\Common\Arr; use Bolt\Configuration\Config; use Bolt\Configuration\Content\ContentType; use Bolt\Doctrine\JsonHelper; @@ -175,8 +176,11 @@ public function shouldReturnSingle(): bool */ public function setParameters(array $params): void { - // array_map('strtolower', $params) to change all params to lowercase. - $this->params = array_filter(array_map('strtolower', $params)); + // Change all params to lowercase, filter out empty ones + $this->params = array_filter(Arr::mapRecursive($params, function ($a) { + return mb_strtolower((string) $a, 'utf-8'); + })); + $this->processFilters(); } diff --git a/src/Twig/TokenParser/SetcontentTokenParser.php b/src/Twig/TokenParser/SetcontentTokenParser.php index f4454b367..3adb0a557 100644 --- a/src/Twig/TokenParser/SetcontentTokenParser.php +++ b/src/Twig/TokenParser/SetcontentTokenParser.php @@ -58,7 +58,9 @@ public function parse(Token $token): Node // where parameter if ($this->parser->getStream()->test(Token::NAME_TYPE, 'where')) { $this->parser->getStream()->next(); - $whereArguments = ['wherearguments' => $this->parser->getExpressionParser()->parseExpression()]; + $whereArguments = [ + 'wherearguments' => $this->parser->getExpressionParser()->parseExpression(), + ]; } // limit parameter