Enhanced Sorting by Nested Meta Values in Pages (#681)
* Added functionality for sorting pages by nested meta values. * Additional variables removed. * Added comment to the config template file about the possibility to access nested metadata with a dot as separator for sorting. * Added entry in the changelog about the possibility to access nested metadata with a dot as separator for sorting. * Changelog and config template adapted according to the suggestions. * Added old entry back in the Config template.
This commit is contained in:
parent
0fa644e64b
commit
869ab1f2e0
@ -32,6 +32,8 @@ Released: -
|
|||||||
now receive the (optional) `$pageId` argument for the new `%page_*%`
|
now receive the (optional) `$pageId` argument for the new `%page_*%`
|
||||||
Markdown placeholders
|
Markdown placeholders
|
||||||
* [New] Add `page()` Twig function to access a page's data
|
* [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
|
* [Changed] ! Pico now requires PHP 7.2.5 or later (this includes full PHP 8
|
||||||
support, also see #528, #534, #608)
|
support, also see #528, #534, #608)
|
||||||
* [Changed] ! Pico now depends on Twig 3.3, skipping Twig 2.x altogether; this
|
* [Changed] ! Pico now depends on Twig 3.3, skipping Twig 2.x altogether; this
|
||||||
|
@ -31,6 +31,7 @@ twig_config: # Twig template engine config
|
|||||||
date_format: "%D %T" # Pico's default date format;
|
date_format: "%D %T" # Pico's default date format;
|
||||||
# See https://php.net/manual/en/function.strftime.php for more info
|
# 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")
|
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_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
|
pages_order: asc # Sort pages in ascending ("asc") or descending ("desc") order
|
||||||
content_dir: ~ # The path to Pico's content directory
|
content_dir: ~ # The path to Pico's content directory
|
||||||
|
15
lib/Pico.php
15
lib/Pico.php
@ -1877,11 +1877,18 @@ class Pico
|
|||||||
if ($orderBy === 'meta') {
|
if ($orderBy === 'meta') {
|
||||||
// sort by arbitrary meta value
|
// sort by arbitrary meta value
|
||||||
$orderByMeta = $this->getConfig('pages_order_by_meta');
|
$orderByMeta = $this->getConfig('pages_order_by_meta');
|
||||||
uasort($this->pages, function ($a, $b) use ($alphaSortClosure, $order, $orderByMeta) {
|
$orderByMetaKeys = explode('.', $orderByMeta);
|
||||||
$aSortValue = isset($a['meta'][$orderByMeta]) ? $a['meta'][$orderByMeta] : null;
|
|
||||||
$aSortValueNull = ($aSortValue === null);
|
|
||||||
|
|
||||||
$bSortValue = isset($b['meta'][$orderByMeta]) ? $b['meta'][$orderByMeta] : null;
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
|
$aSortValueNull = ($aSortValue === null);
|
||||||
$bSortValueNull = ($bSortValue === null);
|
$bSortValueNull = ($bSortValue === null);
|
||||||
|
|
||||||
$cmp = 0;
|
$cmp = 0;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user