From 54eaa25d9236f44b6be456b3beb05926b97a0686 Mon Sep 17 00:00:00 2001 From: Ivo Valchev Date: Wed, 2 Sep 2020 10:01:58 +0200 Subject: [PATCH 1/2] Make order filter work with ContentHelpder::get options --- .../theme/skeleton/custom/setcontent_1.twig | 15 ++++++++++ src/Twig/ArrayExtension.php | 29 ++++++++++++++----- 2 files changed, 37 insertions(+), 7 deletions(-) diff --git a/public/theme/skeleton/custom/setcontent_1.twig b/public/theme/skeleton/custom/setcontent_1.twig index f7869a30b..e4372027e 100644 --- a/public/theme/skeleton/custom/setcontent_1.twig +++ b/public/theme/skeleton/custom/setcontent_1.twig @@ -191,4 +191,19 @@ {% endif %} + +
+

Thirteen

+ {% setcontent showcases = 'showcases' printquery %} + {% set showcases = showcases|order('-floatfield') %} + + Results: {{ showcases|length > 0 ? 'yes' }} +
{% endblock main %} diff --git a/src/Twig/ArrayExtension.php b/src/Twig/ArrayExtension.php index 5e014fc21..8230adf3c 100644 --- a/src/Twig/ArrayExtension.php +++ b/src/Twig/ArrayExtension.php @@ -5,6 +5,7 @@ namespace Bolt\Twig; use Bolt\Entity\Content; +use Bolt\Utils\ContentHelper; use Pagerfanta\Pagerfanta; use Twig\Environment; use Twig\Extension\AbstractExtension; @@ -17,6 +18,14 @@ */ final class ArrayExtension extends AbstractExtension { + /** @var ContentHelper */ + private $contentHelper; + + public function __construct(ContentHelper $contentHelper) + { + $this->contentHelper = $contentHelper; + } + /** * {@inheritdoc} */ @@ -58,21 +67,27 @@ public function length(Environment $env, $thing) /** * Sorts / orders items of an array. */ - public static function order(array $array, string $on = '-datepublish', ?string $onSecondary = null): array + public function order($array, string $on = '-publishedAt', ?string $onSecondary = null): array { - // Set the 'orderOn' and 'orderAscending', taking into account things like '-datepublish'. + if ($array instanceof Pagerfanta) { + $array = (array) $array->getCurrentPageResults(); + } elseif (! is_array($array) && is_iterable($array)) { + $array = (array) $array; + } + + // Set the 'orderOn' and 'orderAscending', taking into account things like '-publishedAt'. [$orderOn, $orderAscending] = self::getSortOrder($on); // Set the secondary order, if any. [$orderOnSecondary, $orderAscendingSecondary] = self::getSortOrder($onSecondary); uasort($array, function ($a, $b) use ($orderOn, $orderAscending, $orderOnSecondary, $orderAscendingSecondary): int { - $check = self::orderHelper($a, $b, $orderOn, $orderAscending); + $check = $this->orderHelper($a, $b, $orderOn, $orderAscending); if ($check !== 0 || $orderOnSecondary !== '') { return $check; } - return self::orderHelper($a, $b, $orderOnSecondary, $orderAscendingSecondary); + return $this->orderHelper($a, $b, $orderOnSecondary, $orderAscendingSecondary); }); return $array; @@ -106,10 +121,10 @@ private static function getSortOrder(?string $name): array /** * Helper function for sorting an array of Content. */ - private static function orderHelper(Content $a, Content $b, string $orderOn, bool $orderAscending): int + private function orderHelper(Content $a, Content $b, string $orderOn, bool $orderAscending): int { - $aVal = $a->getField($orderOn); - $bVal = $b->getField($orderOn); + $aVal = $this->contentHelper->get($a, sprintf('{%s}', $orderOn)); + $bVal = $this->contentHelper->get($b, sprintf('{%s}', $orderOn)); // Check the primary sorting criterion. if ($orderAscending) { From 22da14d05af3b2184e9047af15029e46fa4f6113 Mon Sep 17 00:00:00 2001 From: Ivo Valchev Date: Wed, 2 Sep 2020 10:12:29 +0200 Subject: [PATCH 2/2] Make ordering properly localized --- src/Twig/ArrayExtension.php | 27 ++++++++++++++++++++------- 1 file changed, 20 insertions(+), 7 deletions(-) diff --git a/src/Twig/ArrayExtension.php b/src/Twig/ArrayExtension.php index 8230adf3c..a660c4244 100644 --- a/src/Twig/ArrayExtension.php +++ b/src/Twig/ArrayExtension.php @@ -21,9 +21,17 @@ final class ArrayExtension extends AbstractExtension /** @var ContentHelper */ private $contentHelper; - public function __construct(ContentHelper $contentHelper) + /** @var LocaleExtension */ + private $localeExtension; + + /** @var string */ + private $defaultLocale; + + public function __construct(ContentHelper $contentHelper, LocaleExtension $localeExtension, string $defaultLocale) { $this->contentHelper = $contentHelper; + $this->localeExtension = $localeExtension; + $this->defaultLocale = $defaultLocale; } /** @@ -34,7 +42,7 @@ public function getFilters(): array $env = ['needs_environment' => true]; return [ - new TwigFilter('order', [$this, 'order']), + new TwigFilter('order', [$this, 'order'], $env), new TwigFilter('shuffle', [$this, 'shuffle']), new TwigFilter('length', [$this, 'length'], $env), ]; @@ -67,7 +75,7 @@ public function length(Environment $env, $thing) /** * Sorts / orders items of an array. */ - public function order($array, string $on = '-publishedAt', ?string $onSecondary = null): array + public function order(Environment $twig, $array, string $on = '-publishedAt', ?string $onSecondary = null, $locale = null): array { if ($array instanceof Pagerfanta) { $array = (array) $array->getCurrentPageResults(); @@ -75,19 +83,24 @@ public function order($array, string $on = '-publishedAt', ?string $onSecondary $array = (array) $array; } + if (! $locale) { + $locale = ! empty($this->localeExtension->getHtmlLang($twig)) ? + $this->localeExtension->getHtmlLang($twig) : $this->defaultLocale; + } + // Set the 'orderOn' and 'orderAscending', taking into account things like '-publishedAt'. [$orderOn, $orderAscending] = self::getSortOrder($on); // Set the secondary order, if any. [$orderOnSecondary, $orderAscendingSecondary] = self::getSortOrder($onSecondary); - uasort($array, function ($a, $b) use ($orderOn, $orderAscending, $orderOnSecondary, $orderAscendingSecondary): int { - $check = $this->orderHelper($a, $b, $orderOn, $orderAscending); + uasort($array, function ($a, $b) use ($orderOn, $orderAscending, $orderOnSecondary, $orderAscendingSecondary, $locale): int { + $check = $this->orderHelper($a, $b, $orderOn, $orderAscending, $locale); if ($check !== 0 || $orderOnSecondary !== '') { return $check; } - return $this->orderHelper($a, $b, $orderOnSecondary, $orderAscendingSecondary); + return $this->orderHelper($a, $b, $orderOnSecondary, $orderAscendingSecondary, $locale); }); return $array; @@ -121,7 +134,7 @@ private static function getSortOrder(?string $name): array /** * Helper function for sorting an array of Content. */ - private function orderHelper(Content $a, Content $b, string $orderOn, bool $orderAscending): int + private function orderHelper(Content $a, Content $b, string $orderOn, bool $orderAscending, string $locale): int { $aVal = $this->contentHelper->get($a, sprintf('{%s}', $orderOn)); $bVal = $this->contentHelper->get($b, sprintf('{%s}', $orderOn));