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

Enhanced Sorting by Nested Meta Values in Pages #681

merged 6 commits into from
Dec 2, 2023

Conversation

PxaMMaxP
Copy link

@PxaMMaxP PxaMMaxP commented Dec 1, 2023

This pull request introduces an enhancement to the sorting functionality within pages. Previously, sorting was limited to top-level meta values. With this update, the sorting logic now supports nested meta values, providing greater flexibility and precision in organizing content.

Key Changes:

  • Extended the pages_order_by_meta configuration to interpret dot-separated strings as paths to nested meta values.
  • Updated the sorting algorithm to recursively access nested meta values based on the provided path.

This enhancement ensures a more dynamic and versatile approach to content organization, particularly beneficial for complex page structures with deeply nested meta data.

Copy link
Collaborator

@PhrozenByte PhrozenByte left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for your PR! 👍

LGTM besides some minor things.

Since it's fully backwards compatible we don't need any changes in PicoDeprecated.

lib/Pico.php Outdated
$bMeta = isset($bMeta[$key]) ? $bMeta[$key] : null;
}

$aSortValue = $aMeta;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can't we use a single variable here?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I had to think a bit about what you mean exactly. But it should be simpler and still quite clear.

Thank you for the opportunity to contribute here! It's my first time contributing publicly.

lib/Pico.php Outdated
@@ -1830,11 +1830,22 @@ protected function sortPages()
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.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The code is pretty self-explanatory, I don't think that we need inline code comments here (same with the comment below). However, we do need documentation in config.yml.template and CHANGELOG.md.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have removed the comments. I wasn't quite sure if the comments were right here as good style or not.

@PxaMMaxP
Copy link
Author

PxaMMaxP commented Dec 1, 2023

I am thrilled to hear that my contribution is well-received – it's truly gratifying to see my code being integrated and knowing it will be used by others. Thank you very much for your positive feedback! 😊

Regarding the changelog, I wanted to suggest the following line for inclusion to accurately reflect the new functionality introduced:

Suggested Changelog Entry:

* [New] Enhance `pages_order_by_meta` functionality to allow sorting by nested meta values using '.' notation (e.g., 'author.info')

I apologize for the earlier commit and revert commit confusion. To avoid further complexities, I thought it best to propose the changelog update here. If you'd prefer a separate pull request for this, please let me know, and I'll be happy to arrange that.

Thank you once again for your support and for fostering such a collaborative environment.

@PxaMMaxP PxaMMaxP closed this Dec 1, 2023
@PxaMMaxP PxaMMaxP reopened this Dec 1, 2023
Copy link
Collaborator

@PhrozenByte PhrozenByte left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM 👍

See the minor suggested change below. Your suggested changelog addition looks good, too. Can you please add it to this PR? Will merge it then. Thanks!

@@ -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')
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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_meta: author # Sort pages by meta value "author" (set "pages_order_by" to "meta")
# Use '.' notation for nested meta keys (e.g. 'author.info')

@PxaMMaxP
Copy link
Author

PxaMMaxP commented Dec 1, 2023

A little tinkering led to success: The changelog has now been changed accordingly!

Thanks again for your patience!

@PhrozenByte
Copy link
Collaborator

Looks like there went something wrong, the line wrapping in config.yml.template was lost again (I assume because you didn't pull after you've committed the change via GitHub's "Commit suggestion" feature; you can use git pull --rebase if you have unpushed commits locally). While you're at it, can you also add line wraps in CHANGELOG.md matching the line length of the other entries as well? Thanks again! 👍

@PxaMMaxP
Copy link
Author

PxaMMaxP commented Dec 2, 2023

Thanks again for your patience!

It should be correct now, right?

Copy link
Collaborator

@PhrozenByte PhrozenByte left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, great work, thank you very much! ❤️ 🎉 👍

Don't worry about the failing CI btw... 😉

@PhrozenByte PhrozenByte merged commit 869ab1f into picocms:pico-3.0 Dec 2, 2023
0 of 7 checks passed
@PxaMMaxP PxaMMaxP deleted the nested-meta-sorting branch December 2, 2023 13:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants