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

Make order filter work with ContentHelper::get() options #1809

Merged
merged 2 commits into from
Sep 2, 2020
Merged
Show file tree
Hide file tree
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
15 changes: 15 additions & 0 deletions public/theme/skeleton/custom/setcontent_1.twig
Original file line number Diff line number Diff line change
Expand Up @@ -191,4 +191,19 @@
{% endif %}
</ul>
</section>

<section id="thirteen">
<h1>Thirteen</h1>
{% setcontent showcases = 'showcases' printquery %}
{% set showcases = showcases|order('-floatfield') %}

Results: <span id="results-nine">{{ showcases|length > 0 ? 'yes' }}</span>
<ul>
{% for showcase in showcases %}
<li>showcase {{ showcase.id }}: {{ showcase.floatfield }}
<span class="s{{ loop.index }}">{{ _self.issmaller(showcase.floatfield, last|default()) }}</span>
</li>
{% set last = showcase.floatfield %}
{% endfor %}
</section>
{% endblock main %}
46 changes: 37 additions & 9 deletions src/Twig/ArrayExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -17,6 +18,22 @@
*/
final class ArrayExtension extends AbstractExtension
{
/** @var ContentHelper */
private $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;
}

/**
* {@inheritdoc}
*/
Expand All @@ -25,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),
];
Expand Down Expand Up @@ -58,21 +75,32 @@ 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(Environment $twig, $array, string $on = '-publishedAt', ?string $onSecondary = null, $locale = 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;
}

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 = self::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 self::orderHelper($a, $b, $orderOnSecondary, $orderAscendingSecondary);
return $this->orderHelper($a, $b, $orderOnSecondary, $orderAscendingSecondary, $locale);
});

return $array;
Expand Down Expand Up @@ -106,10 +134,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, string $locale): 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) {
Expand Down