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

Enhanced Sorting by Nested Meta Values in Pages #681

Merged
merged 6 commits into from
Dec 2, 2023
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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ Released: -
now receive the (optional) `$pageId` argument for the new `%page_*%`
Markdown placeholders
* [New] Add `page()` Twig function to access a page's data
* [New] Enhance `pages_order_by_meta` functionality to allow sorting by
nested meta values using '.' notation (e.g., 'author.info')
* [Changed] ! Pico now requires PHP 7.2.5 or later (this includes full PHP 8
support, also see #528, #534, #608)
* [Changed] ! Pico now depends on Twig 3.3, skipping Twig 2.x altogether; this
Expand Down
1 change: 1 addition & 0 deletions config/config.yml.template
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ twig_config: # Twig template engine config
date_format: "%D %T" # Pico's default date format;
# See https://php.net/manual/en/function.strftime.php for more info
pages_order_by_meta: author # Sort pages by meta value "author" (set "pages_order_by" to "meta")
# Use '.' notation for nested meta keys (e.g. 'author.info')
pages_order_by: alpha # Change how Pico sorts pages ("alpha" for alphabetical order, "date", or "meta")
pages_order: asc # Sort pages in ascending ("asc") or descending ("desc") order
content_dir: ~ # The path to Pico's content directory
Expand Down
15 changes: 11 additions & 4 deletions lib/Pico.php
Original file line number Diff line number Diff line change
Expand Up @@ -1877,11 +1877,18 @@ protected function sortPages(): void
if ($orderBy === 'meta') {
// sort by arbitrary meta value
$orderByMeta = $this->getConfig('pages_order_by_meta');
uasort($this->pages, function ($a, $b) use ($alphaSortClosure, $order, $orderByMeta) {
$aSortValue = isset($a['meta'][$orderByMeta]) ? $a['meta'][$orderByMeta] : null;
$aSortValueNull = ($aSortValue === null);
$orderByMetaKeys = explode('.', $orderByMeta);

uasort($this->pages, function ($a, $b) use ($alphaSortClosure, $order, $orderByMetaKeys) {
$aSortValue = $a['meta'];
$bSortValue = $b['meta'];

foreach ($orderByMetaKeys as $key) {
$aSortValue = isset($aSortValue[$key]) ? $aSortValue[$key] : null;
$bSortValue = isset($bSortValue[$key]) ? $bSortValue[$key] : null;
}

$bSortValue = isset($b['meta'][$orderByMeta]) ? $b['meta'][$orderByMeta] : null;
$aSortValueNull = ($aSortValue === null);
$bSortValueNull = ($bSortValue === null);

$cmp = 0;
Expand Down
Loading