When trying to use natural sort in a sort criteria that is exposed and results are paged with AJAX enabled, loading subsequent pages fails with a PHP error Illegal choice NASC in Order element.. The values NASC and NDESC are failing validation in the exposed form validator.
Steps to reproduce:
- Minimal site install
$ drush si minimal - Enable modules views, views_ui, views_natural_sort
$ drush en views, views_ui, views_natural_sort - Create a content type
- Create enough content of that type to be able to page results (I used devel_generate, but theoretically 2 should be enough)
- Create a block view of an unformatted list of the content type
- Add Sort Criteria of the Content's Title. Check radio to
Sort ascending naturallyandExpose the sort... Use pager: Miniand set the page size to smaller than the total number pieces of content you created- Enable
Use AJAX: Yes - Save the view
- Flush the cache
$ drush cr - In preview, try to load the second page of results
- The page should not load, and there should be an
Errorin recent logs
| Comment | File | Size | Author |
|---|---|---|---|
| #11 | 30834235-11.patch | 27.41 KB | ts.ag |
Issue fork views_natural_sort-3083423
Show commands
Start within a Git clone of the project using the version control instructions.
Or, if you do not have SSH keys set up on git.drupalcode.org:
Comments
Comment #2
generalredneckI'm not able to reproduce this today. Can you try again and let me know if it's still an issue? Feel free to reopen if you can. Here's my attempt.

Comment #3
generalredneckSo I see what I did wrong... I didn't expose the sort... I think that's a more pressing issue from the "pager not working on ajax"... Going to reopen and change the title
kinda was following the symptom and got caught up in it.
Comment #4
generalredneckI've run out of time to be able to track this one down today... but here's what I found...
In core/modules/views/src/Plugin/views/exposed_form/ExposedFormPluginBase.php, there's a function exposedFormAlter. It hard-codes the sort orders on lines 220-223 to just asc and desc. I'm not 100% sure how to get around this besides to check to see if someone chose to sort things naturally by asc or desc and then translate the asc and desc options to the proper counterpart. I think this may require some restructure from having NDESC and NASC to having a separte option that just says "Sort naturally" and check that... It does mean that likely I won't be able to allow people to expose whether to sort naturally or not... but they could at least decide if it's to be ASC or DESC and the naturally part is just on or off based on the person who configured the view.
Something like this:

Comment #5
sker101 commentedConfirmed that this is still an issue.
According to comment #4, since it's an issue due to the sorting method being hardcoded in the base plugin file of exposed form, I created a workaround patch which creates a new exposed form plugin named "Views Natural Sort" and allows the options `NASC` and `NDESC` to be accepted for sorting.
Comment #6
generalredneck@sker101,
Interesting idea. I kinda wish there was a way to "force" it to happen when someone has a naturally sorted as to remove the element of "mis-configuration".
I'll have to dig back into this one to understand the flow... But then again, I may have to just accept this gotcha without a major rewrite of the NASC/NDESC logic.
Comment #7
generalredneckSo looking at this... Is there any usecase where someone would want to be able to select between all 4 of Sort Ascending, Descending, Ascending Naturally, or Descending Naturally?
If not, I think the solution to this is to remove the option to select from the 4 different options as defined here:

Then we just add a checkmark option to that form "Sort Naturally" and go off of that. That should add a new option that we could flag on and it would then be up to the sort handler to make sure that the sorting happens naturally or not based on that instead of if there is an "N" in front of the option.
Then the exposed filter would sort natually if the option was checked or not if it was unchecked but you couldn't flip flop between the two without extra work.
Comment #8
generalredneck@sker101,

I actually took a different approach all together from what you had in your patch to keep from having to have people go through and manually configure the exposed form filter. It's all based on the fact that we will never have 4 options in that Sort Order dropdown. In the future we might expand the exposed form to allow the new "Sort Naturally" checkbox, but for now, the exposed filter sorts Ascending or Descending either Naturally or Normally based on if the Sort Naturally checkbox is checked in the view configuration. Here's what the new form looks like
This new patch does the following:
Things I've tested...
To give this a shot:
Comment #9
hershey.k commented@generalredneck - I applied the patch on our site and am seeing the update db fail with the following output. See attached.
Tech specs:
Drupal: v10.1.3
VNS: v2.0.0-alpha7
PHP: v8.1
FYI - Patch #5 applies okay and addresses the issue for our use case.
Comment #10
cslevy commentedI couldn't apply the patch from #8 so I recreated the patch to work with the latest version of the module
Comment #11
ts.ag commentedModified patch from #10 to fix PHP 8.4 deprecation warning in the definition of init().
Comment #13
generalrednecklooks like several people are using it well.
Comment #17
generalredneckIt turns out that I wrote similar functionality over in #3442536: How to safely uninstall? and the use cases overlap. That one's going to get merged though.
It's going to suck for those of you using this patch as you will have to migrate this configuration over to the new one. Someone may want to write a patch that does a configuration update from this to what is being released.
Also we will need to test again to double check this issue is fixed with what was merged.
Comment #18
generalredneckOk so After pushing #3442536: How to safely uninstall? this issue is fixed.