Problem/Motivation
Followup from #2651102: Using checkboxes for exposed filters results in zero rows displaying, this was originally reported in the Better Exposed Filters issue queue, but has since been moved to Drupal core.
When using checkboxes to render exposed form options, the FormAPI returns unchecked checkboxes as 0 => 0 entries. This doesn't cause any problems with OR filter ("is any of") but does with AND filters ("is all of").
Steps to reproduce:
- Standard install (
drush si standard -y --account-pass=admin) with some devel-generated content - Create a new view and add the "Content: Has taxonomy term" filter, select the Tags vocabulary and Dropdown for the selection type, followed by "Apply and continue"
- Tick the "Expose" option
- Set the operator to "Is all of" and tick the "Allow multiple selections" option and click the Apply button
- Either add a form_alter similar to this
function test_form_views_exposed_form_alter(&$form, \Drupal\Core\Form\FormStateInterface $form_state) { $form['tid']['#type'] = 'checkboxes'; }or enable the Better Exposed Filters module and set this filter to render as checkboxes.
- Select any of the term checkboxes and click Apply
No results are returned. If you look at the generated SQL you will see something similar to
INNER JOIN {taxonomy_index} taxonomy_index_value_0 ON node_field_data.nid = taxonomy_index_value_0.nid AND taxonomy_index_value_0.tid = '5'
INNER JOIN {taxonomy_index} taxonomy_index_value_1 ON node_field_data.nid = taxonomy_index_value_1.nid AND taxonomy_index_value_1.tid = '0'
Proposed resolution
Filter user input from the form state to remove the data of empty checkboxes.
Remaining tasks
- Determine where to fix this issue: the
ManyToOnefilter handler orViewsExposedForm::submitForm. See the patches in #23 and #24 - Tests
User interface changes
Nope.
API changes
Nope.
Data model changes
Nope.
| Comment | File | Size | Author |
|---|---|---|---|
| #38 | 2687773-38-checkboxes.patch | 14.39 KB | mikeker |
| #38 | 2687773-35-38.interdiff.txt | 1.63 KB | mikeker |
| #35 | 2687773-35-checkboxes.patch | 14.43 KB | mikeker |
| #35 | 2687773-31-35.interdiff.txt | 754 bytes | mikeker |
| #31 | 2687773-checkboxes.patch | 14.43 KB | mikeker |
Comments
Comment #2
amateescu commentedThis is one way to handle it in the module.
Comment #3
geerlingguy commentedJust a quick +1 to indicate I'm seeing this problem too. For Google-ability, this is the error I get when I try using the pager on a BEF-powered views filter with a bunch of checkboxes where none are checked:
"An illegal choice has been detected. Please contact the site administrator."
Comment #4
luksakI have the same issue as described in #3 when using the "Remember the last selection" option for a filter using checkboxes. In the watchdog I get this message:
Illegal choice 0 in field_tags_target_id element.In the frontend I get this message:
An illegal choice has been detected. Please contact the site administrator.The patch in #2 doesn't fix this issue.
Comment #5
mikeker commentedApologies for the delay getting to this issue... I appreciate your patience! (And thank you, @Lukas von Blarer, for pinging me in the other issue.)
Using the latest 8.2.x core and BEF, I'm not able to reproduce the original issue. (Though it's been long enough since it was reported that it may have been fixed by a change in core?). But I am getting a variation on it. STR:
When I navigate to the page for this view and click around in the pager it works as expected. If I select a checkbox and apply it, it works as expected for the initial page. If I then click a pager link, I get something similar to
letters%5Ba%5D=a&a=a&page=1(note the extra&a=a, which continues for each option checked). The patch in #2 doesn't remove the additional&a=a.Can someone provide steps to reproduce the Cascade-of-Zeros or confirm that it's no longer happening? Thanks.
Comment #6
luksakYes, this exactly how it works for me as well by reproducting the steps described. I also attached the exported view to make it easier to reproduce. There is a step missing in your instructions. The issue I am facing is when having the "Remember the last selection" option active for the filter. After activating that remove the GET parameters from the URL and you should get the error I described.
Comment #7
dawehnerI needed something along these lines ... to at least get rid of the fatal.
Comment #8
luksakWow, that solved the issue for me. Thank you!
What is missing to fix this properly?
Comment #9
geerlingguy commented@dawehner - Your patch works great for both the view I was initially having trouble with, as well as another view that I had the same problem with but on a different type of filter. So thanks!
+1 from me, please merge to make the module usable without the patch for exposed checkboxes/multi-select.
Comment #10
dawehnerOf course it would be super nice to have some tests ...
Comment #11
amateescu commentedWe should also check to see if this patch actually belongs in core, given that #2651102: Using checkboxes for exposed filters results in zero rows displaying was fixed there.
Comment #12
dawehner@amateescu
That is a fair question! I guess we would want to apply this cleanup just for GET forms.
Comment #13
luksak@ameteescu Yes I also thought about that... Would make sense...
This patch fixes the error message, but the functionality is inconsistent. Sometimes it works, sometimes it doesn't. I can't really see any logic in which cases it breaks. Any ideas?
Comment #14
luksakComment #15
sukanya.ramakrishnan commentedAll, Can this issue be fixed in core ? I am not using BEF and i am facing the same issue!
Thanks,
Sukanya
Comment #16
sukanya.ramakrishnan commentedSubmitted a patch for this issue at https://www.drupal.org/node/2651102#comment-11542843
Please review and let me know if it works!
Thanks
Sukanya
Comment #17
sukanya.ramakrishnan commentedChanging project to core to provide a fix based on changes made for 2651102 - using checkboxes for exposed filters results in zero results displaying.
Comment #18
sukanya.ramakrishnan commentedSubmitting a patch for core. My proposed fix is based upon my debugging and subsequent discovery that $view-> exposed_raw_input doesnt contain the checkbox values in the right format which causes form_state to not retain values of the checkbox properly upon pager navigation.
Submitting a patch for 8.1 and 8.2(both are essentially the same but still submitting two patches to avoid confusion).
8.1 should be applied only after applying https://www.drupal.org/node/2651102 patch from #83.
Thanks!
Sukanya
Comment #19
dawehnerComment #20
sukanya.ramakrishnan commentedComment #21
sukanya.ramakrishnan commentedComment #23
mroycroft commentedUpdated patch for 8.2, there were problems introduced when there were multiple exposed checkbox filters with values set in each filter.
What was happening was that after each checkbox filter was processed, the $checked_array variable persisted values from previous filters. I've uploaded an updated patch file with the proposed fix for #18 attached.
Comment #24
mikeker commentedWhat about filtering out unchecked checkboxes at the ManyToOne filter handler instead?
No interdiff since this is a completely different approach. Curious what the testbot thinks, if it comes back green I'll work on tests.
Comment #25
mikeker commentedWhat about filtering out unchecked checkboxes at the ManyToOne filter handler instead?
No interdiff since this is a completely different approach. Curious what the testbot thinks, if it comes back green I'll work on tests.
This approach has the side effect of fixing #2821515: "is all of" filter operator doesn't work with checkboxes as well, which makes my life better! :)
Comment #26
mikeker commentedNo idea why it double-posted when I was just trying to edit... Sorry about the noise.
Comment #27
lendudeIs the IS still up to date? I see some things about not being able to reproduce the original issue and some new issues being introduced in 8.2. The title makes this look like a task, but it's a bug.
Some clear steps to reproduce or a test would really help show what we are trying to fix here.
Comment #28
mikeker commentedNope, it was way out of date but should be better now. Add STR and noted the two possible options to fix the issue. Also retitled to clarify the issue.
Thanks for pointing that out @Lendude.
Comment #29
mikeker commentedComment #30
lendude@mikeker thanks for the IS update, makes much more sense now!
I like the look of the fix in #24, it could use an inline comment about why we are doing that, and it obviously still needs tests, but that looks like a good direction to take.
Comment #31
mikeker commentedSorry for taking so long to get back to this issue...
Attached is a tests-only patch which should highlight the issue and the patch from #24 (plus an inline comment, as requested by @Lendude). The tests-only patch is essentially the interdiff.
Comment #32
mikeker commentedComment #35
mikeker commentedThe failure in #31 seems to be unrelated. Regardless, I've made the coding standards change in this patch.
Forgot to mention in #31, that I refactored the checkboxes tests into their own files as I didn't want to view changes to affect the other exposed form tests.
Comment #37
lendude@mikeker++
just some nits:
Lets not use a fully qualified name here. (See all the other uses of EntityReferenceTestTrait).
Can just be @inheritdoc
Can just be @inheritdoc
lets use self::class instead of get_class, it's what the cool kids do I've been told ;)
missing period at the end.
Comment #38
mikeker commented@Lendude, thank you for the review! Nits have been picked. :)
Comment #39
lendude@mikeker thanks for pickin' them!
Comment #43
catchCommitted/pushed to 8.5.x and cherry-picked to 8.4.x. Thanks!