From 711897e6190c97706b4cfd08a5e452f869e1b765 Mon Sep 17 00:00:00 2001 From: Max P Date: Fri, 1 Dec 2023 14:25:48 +0100 Subject: [PATCH 1/6] Added functionality for sorting pages by nested meta values. --- lib/Pico.php | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/lib/Pico.php b/lib/Pico.php index ae303854..c2530f7f 100644 --- a/lib/Pico.php +++ b/lib/Pico.php @@ -1877,11 +1877,22 @@ 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; + // Split the configuration value into an array of keys to allow sorting by nested meta values. + $orderByMetaKeys = explode('.', $orderByMeta); + + uasort($this->pages, function ($a, $b) use ($alphaSortClosure, $order, $orderByMetaKeys) { + $aMeta = $a['meta']; + $bMeta = $b['meta']; + // Iterate through the meta keys to drill down into nested arrays for the final sort value. + foreach ($orderByMetaKeys as $key) { + $aMeta = isset($aMeta[$key]) ? $aMeta[$key] : null; + $bMeta = isset($bMeta[$key]) ? $bMeta[$key] : null; + } + + $aSortValue = $aMeta; $aSortValueNull = ($aSortValue === null); - $bSortValue = isset($b['meta'][$orderByMeta]) ? $b['meta'][$orderByMeta] : null; + $bSortValue = $bMeta; $bSortValueNull = ($bSortValue === null); $cmp = 0; From c94e384ba30e5539cffd02b9b538990fe6808bdd Mon Sep 17 00:00:00 2001 From: Max P Date: Fri, 1 Dec 2023 18:26:58 +0100 Subject: [PATCH 2/6] Additional variables removed. --- lib/Pico.php | 14 +++++--------- 1 file changed, 5 insertions(+), 9 deletions(-) diff --git a/lib/Pico.php b/lib/Pico.php index c2530f7f..653380f3 100644 --- a/lib/Pico.php +++ b/lib/Pico.php @@ -1877,22 +1877,18 @@ protected function sortPages(): void if ($orderBy === 'meta') { // sort by arbitrary meta value $orderByMeta = $this->getConfig('pages_order_by_meta'); - // Split the configuration value into an array of keys to allow sorting by nested meta values. $orderByMetaKeys = explode('.', $orderByMeta); uasort($this->pages, function ($a, $b) use ($alphaSortClosure, $order, $orderByMetaKeys) { - $aMeta = $a['meta']; - $bMeta = $b['meta']; - // Iterate through the meta keys to drill down into nested arrays for the final sort value. + $aSortValue = $a['meta']; + $bSortValue = $b['meta']; + foreach ($orderByMetaKeys as $key) { - $aMeta = isset($aMeta[$key]) ? $aMeta[$key] : null; - $bMeta = isset($bMeta[$key]) ? $bMeta[$key] : null; + $aSortValue = isset($aSortValue[$key]) ? $aSortValue[$key] : null; + $bSortValue = isset($bSortValue[$key]) ? $bSortValue[$key] : null; } - $aSortValue = $aMeta; $aSortValueNull = ($aSortValue === null); - - $bSortValue = $bMeta; $bSortValueNull = ($bSortValue === null); $cmp = 0; From 0092429793acfedaad7f0f08b6a6015b2a9f8029 Mon Sep 17 00:00:00 2001 From: Max P Date: Fri, 1 Dec 2023 18:35:09 +0100 Subject: [PATCH 3/6] Added comment to the config template file about the possibility to access nested metadata with a dot as separator for sorting. --- config/config.yml.template | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/config/config.yml.template b/config/config.yml.template index 2a5647d6..be40dda2 100644 --- a/config/config.yml.template +++ b/config/config.yml.template @@ -30,7 +30,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") +pages_order_by_meta: author # Sort pages by meta value "author" (set "pages_order_by" to "meta"); use '.' 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 From 987cf5ffc2a5696afb6f4294f513cba7c1e75106 Mon Sep 17 00:00:00 2001 From: Max P Date: Fri, 1 Dec 2023 19:58:13 +0100 Subject: [PATCH 4/6] Added entry in the changelog about the possibility to access nested metadata with a dot as separator for sorting. --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 43ade98e..49b901d7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -32,6 +32,7 @@ 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 From e995d5a51c98326af68f14dbedf5b6b6c1774fea Mon Sep 17 00:00:00 2001 From: Max P Date: Sat, 2 Dec 2023 14:13:10 +0100 Subject: [PATCH 5/6] Changelog and config template adapted according to the suggestions. --- CHANGELOG.md | 3 ++- config/config.yml.template | 4 ++-- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 49b901d7..30710981 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -32,7 +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') +* [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 diff --git a/config/config.yml.template b/config/config.yml.template index be40dda2..4e329626 100644 --- a/config/config.yml.template +++ b/config/config.yml.template @@ -30,8 +30,8 @@ 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 '.' 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_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: asc # Sort pages in ascending ("asc") or descending ("desc") order content_dir: ~ # The path to Pico's content directory content_ext: .md # The file extension of your Markdown files From 9fde594e5cb705b82b905f1295c667ee7bed488c Mon Sep 17 00:00:00 2001 From: Max P Date: Sat, 2 Dec 2023 14:13:10 +0100 Subject: [PATCH 6/6] Added old entry back in the Config template. --- config/config.yml.template | 1 + 1 file changed, 1 insertion(+) diff --git a/config/config.yml.template b/config/config.yml.template index 4e329626..37d06ebb 100644 --- a/config/config.yml.template +++ b/config/config.yml.template @@ -32,6 +32,7 @@ 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 content_ext: .md # The file extension of your Markdown files