Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix |filter and |order chaining returning no results #2603

Merged
merged 2 commits into from
Jun 2, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 8 additions & 6 deletions src/Twig/ArrayExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Bolt\Entity\Field\NumberField;
use Bolt\Utils\ContentHelper;
use Carbon\Carbon;
use Iterator;
use Pagerfanta\Pagerfanta;
use Twig\Environment;
use Twig\Extension\AbstractExtension;
Expand Down Expand Up @@ -79,11 +80,7 @@ public function length(Environment $env, $thing)
*/
public function order(Environment $twig, $array, string $on = '-publishedAt', ?string $onSecondary = null, $locale = null): array
{
if ($array instanceof Pagerfanta) {
$array = (array) $array->getCurrentPageResults();
} elseif (! is_array($array) && is_iterable($array)) {
$array = (array) $array;
}
$array = $this->getArray($array);

if (! $locale) {
$locale = ! empty($this->localeExtension->getHtmlLang($twig)) ?
Expand Down Expand Up @@ -162,7 +159,12 @@ private function orderHelper(Content $a, Content $b, string $orderOn, bool $orde
private function getArray($array)
{
if ($array instanceof Pagerfanta) {
return (array) $array->getCurrentPageResults();
$array = (array) $array->getCurrentPageResults();
} elseif ($array instanceof Iterator) {
// Special edge-case for "|filter. See #2601
$array = iterator_to_array($array);
} elseif (! is_array($array) && is_iterable($array)) {
$array = (array) $array;
}

return $array;
Expand Down