Problem/Motivation
I am attempting to update my view's filter criteria to something like (A and B and C) and (D or E or F).
The admin UI in the modal pop-up appears to work: I can add a new filter group, drag the specific filters to the right group, and change the ANDs and ORs — but when I click the "Apply (all displays)" button, all of my updates disappear. I land back to the main admin screen and my changes are gone.
I also see an error in the watchdog log:
- Type: form
- Location: .../admin/structure/views/ajax/rearrange-filter/VIEWNAME/webform_select_1/filter
- Referrer: .../admin/structure/views/view/VIEWNAME/edit/webform_select_1
- Message: Illegal choice 2 in filters element.
- Severity: error
This used to work; I've used this functionality relatively recently. And older views with more complicated filter groups and operators continue to work correctly, if they were created before this broke. So something must have changed fairly recently.
Does anybody have any ideas what might be causing this problem?
Steps to reproduce
- Create a view with multiple filter criteria
- Next to "Filter criteria", click "And/Or, Rearrange"
- In the modal pop-up click "+ Create new filter group"
- Drag a couple of the filter criteria to the new filter group
- Click "Apply (all displays)" or "Apply (this display)"
- Your changes are gone.
| Comment | File | Size | Author |
|---|---|---|---|
| Screenshot 2023-03-17 at 15-05-31 Courses 2 (Content) UC Berkeley School of Information.png | 55.53 KB | berkeleyjon |
Issue fork views-3348800
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
dcloganguy commentedWe are having the same issue.
Drupal 7.95
Views 7.x-3.27
If I try to filter something like below, it does not save and throws an error. Steps to reproduce are the same as above.
Error:form Illegal choice 2 in filters element.
Content: Published status No
And
Content: Type = Basic page
OR
Content: Type = Book page
thanks
Comment #3
rclemings commentedSame here with Drupal 7.95 and Views 7.x-3.29, and with Views 7.x-3.28 on another site.
It looks as if this has been a problem for some time, if unnoticed until now. I rolled back as far as 3.25 and it was still an issue, as far as I could tell.
Comment #4
rimu commentedI recently upgraded to PHP 8.2 from 7.2, maybe that is related.
Comment #5
damienmckennaAre you all using PHP 8.2?
Comment #6
rclemings commentedPHP Version 8.1.18 here.
Comment #7
berkeleyjon commentedPHP 8.1.18 here, too.
Comment #8
wrg3@calvin.edu commentedWe're seeing this as well. Our system:
FWIW, we were able to reproduce this on every PHP 8.1-compatible Views release.
We downgraded our PHP version to 7.4 and this immediately began working again.
Comment #9
wrg3@calvin.edu commentedWe just attempted a PHP downgrade, which resolved the issue.
Curiously, once we downgraded, an empty filter group was immediately visible.
Comment #10
damienmckennaSounds like a PHP 8.1 compatibility bug then.
Comment #11
attekilpela commentedI checked this and probably found the bug, but I'm not sure where it actually is:
1. User click Create new filter group and AJAX is executed to menu callback $items['admin/structure/views/ajax/%/%views_ui_cache']
https://github.com/drupalprojects/views/blob/7.x-3.x/views_ui.module#L135
2. Function views_ui_ajax_form is executed with parameters: $js = TRUE, $key = rearrange-filter, $view = views name
https://github.com/drupalprojects/views/blob/7.x-3.x/includes/admin.inc#...
3. $key is reassigned in line 3015 $key = key($view->stack); and is assigned to zero.
https://github.com/drupalprojects/views/blob/7.x-3.x/includes/admin.inc#...
4. Then comparison $view->form_cache['key'] != $key where values are rearrange-filter and 0 which in PHP7 is FALSE and PHP8 is TRUE. The comparison of course should be between rearrange-filter and rearrange-filter.
https://github.com/drupalprojects/views/blob/7.x-3.x/includes/admin.inc#...
Comment #12
kristofferwiklund commentedFinding the same problem. Looking in to it. But it seems the the references @attekilpela has, is to an old github version of Views. The correct urls should be to here: https://git.drupalcode.org/project/views/-/blob/7.x-3.x/includes/admin.inc
Comment #13
kristofferwiklund commentedSo we have found code that seems have never been run in PHP 7. But as logic in PHP 8 has been changed (to the better) its now running and breaks views.
In code admin.inc:3096 https://git.drupalcode.org/project/views/-/blob/7.x-3.29/includes/admin.... we have the following row:
if (isset($view->form_cache) && $view->form_cache['key'] != $key) {
unset($view->form_cache);
}
When adding a filter group $view->form_cache['key'] will be "rearrange-filter" and $key will be "0"
And in PHP 7: $view->form_cache['key'] != $key is FALSE. As "rearrange-filter" is converted to int,0. So 0!=0 is FALSE.
And in PHP 8: $view->form_cache['key'] != $key is TRUE. As $key is converted to string ''. So "rearrange-filter" != '' is TRUE.
(More info here: https://wiki.php.net/rfc/string_to_number_comparison#introduction)
The affect of this is that that form_cache is clear the the form is not updated.
I have tried setting a break point in PHP7 on the "unset($view->form_cache);" row. And that has never been triggered for me.
So the solution for me is to remove the if code.
Comment #15
joelpittetThis change is working well. Thanks @kristofferwiklund for digging into the details.
Comment #16
damienmckennaFYI that code section comes from #118672: Adding sql ORing capability with filters and hasn't changed in 13 years.
Comment #17
damienmckennaComment #18
damienmckennaCommitted. Thank you everyone!