Skip to content

DataViews: filterSortAndPaginate should ignore sorting on non-sortable fields#73950

Merged
ntsekouras merged 2 commits into
WordPress:trunkfrom
fushar:defensive-sorting
Dec 15, 2025
Merged

DataViews: filterSortAndPaginate should ignore sorting on non-sortable fields#73950
ntsekouras merged 2 commits into
WordPress:trunkfrom
fushar:defensive-sorting

Conversation

@fushar
Copy link
Copy Markdown
Contributor

@fushar fushar commented Dec 12, 2025

What?

filterSortAndPaginate() should check whether the requested sort-by field is sortable or not before attempting to sort the data.

Why?

useView() will retrieve the previously-persisted view.

The following faulty sequence of events could happen:

  • We (developer) have defined the set of fields.
  • A user sorts by a field, which will then persist the view.
  • Later, we realize that the data could contain value for that field that cannot be sorted (can't call localeCompare() on it). Or we just decide that the field should not be sortable.
  • We mark the field as enableSorting: false
  • The user comes back, and persisted view will be retrieved.
  • filterSortAndPaginate() runs, and could potentially crash. Example error:
Cannot read properties of undefined (reading 'localeCompare')

How?

Check for field.enableSorting first before sorting.

Testing Instructions

Verify the added unit test passes.

@fushar fushar marked this pull request as ready for review December 12, 2025 06:37
@github-actions
Copy link
Copy Markdown

github-actions Bot commented Dec 12, 2025

The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the props-bot label.

If you're merging code through a pull request on GitHub, copy and paste the following into the bottom of the merge commit message.

Co-authored-by: fushar <fushar@git.wordpress.org>
Co-authored-by: ntsekouras <ntsekouras@git.wordpress.org>
Co-authored-by: oandregal <oandregal@git.wordpress.org>

To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook.

@ntsekouras ntsekouras added [Type] Bug An existing feature does not function as intended [Feature] DataViews Work surrounding upgrading and evolving views in the site editor and beyond labels Dec 12, 2025
const sortByField = view.sort?.field
? _fields.find( ( field ) => {
return field.id === view.sort?.field;
return (
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This makes sense to me. @oandregal should we add the same check for groupByField?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I suppose this is fine.

@fushar can you share more in which situations do you run into this? If the field is not sortable, it won't be listed in the view config sort component, and so the user will never trigger a "sorting request" with that field.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Just checked the PR description and it's for covering for stored views.

@fushar
Copy link
Copy Markdown
Contributor Author

fushar commented Dec 15, 2025

Can we merge this? cc: @youknowriad since it's a bit related to view persistence (I can't add reviewer since I don't have the rights) 😄

@ntsekouras
Copy link
Copy Markdown
Contributor

ntsekouras commented Dec 15, 2025

Can we merge this?

@fushar it seems @oandregal agreed (thumbs up emoji) that we should also add this check for groupByField.

@fushar
Copy link
Copy Markdown
Contributor Author

fushar commented Dec 15, 2025

Can we merge this?

@fushar it seems @oandregal agreed (thumbs up emoji) that we should also add this check for groupByField.

Oops, I misunderstood the comment. I am just aware of the groupBy feature now 😄 Sure, added it via the latest commit!

Copy link
Copy Markdown
Contributor

@ntsekouras ntsekouras left a comment

Choose a reason for hiding this comment

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

LGTM, thanks!

@ntsekouras ntsekouras merged commit 378df51 into WordPress:trunk Dec 15, 2025
36 checks passed
@github-actions github-actions Bot added this to the Gutenberg 22.4 milestone Dec 15, 2025
@fushar
Copy link
Copy Markdown
Contributor Author

fushar commented Dec 16, 2025

Thank you!!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

[Feature] DataViews Work surrounding upgrading and evolving views in the site editor and beyond [Type] Bug An existing feature does not function as intended

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants