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:

  1. Standard install (drush si standard -y --account-pass=admin) with some devel-generated content
  2. 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"
  3. Tick the "Expose" option
  4. Set the operator to "Is all of" and tick the "Allow multiple selections" option and click the Apply button
  5. 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.

  6. 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 ManyToOne filter handler or ViewsExposedForm::submitForm. See the patches in #23 and #24
  • Tests

User interface changes

Nope.

API changes

Nope.

Data model changes

Nope.

Comments

amateescu created an issue. See original summary.

amateescu’s picture

Status: Active » Needs review
StatusFileSize
new901 bytes

This is one way to handle it in the module.

geerlingguy’s picture

Just 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."

luksak’s picture

Status: Needs review » Needs work

I 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.

mikeker’s picture

Apologies 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:

  1. Enable the bef_test module and generate a bunch of "BEF Test" content using Devel
  2. Create a view with an exposed "bef_letters" filter, tick "Allow multiple selections"
  3. Enable BEF and set the bef_letters filter to render using checkboxes
  4. Change the pager to a full pager showing 5 items

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.

luksak’s picture

StatusFileSize
new25.41 KB

Yes, 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.

dawehner’s picture

StatusFileSize
new985 bytes

I needed something along these lines ... to at least get rid of the fatal.

luksak’s picture

Wow, that solved the issue for me. Thank you!

What is missing to fix this properly?

geerlingguy’s picture

Status: Needs work » Needs review

@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.

dawehner’s picture

Of course it would be super nice to have some tests ...

amateescu’s picture

We 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.

dawehner’s picture

@amateescu
That is a fair question! I guess we would want to apply this cleanup just for GET forms.

luksak’s picture

@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?

luksak’s picture

Status: Needs review » Needs work
sukanya.ramakrishnan’s picture

All, Can this issue be fixed in core ? I am not using BEF and i am facing the same issue!

Thanks,
Sukanya

sukanya.ramakrishnan’s picture

Submitted 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

sukanya.ramakrishnan’s picture

Project: Better Exposed Filters » Drupal core
Version: 8.x-3.x-dev » 9.x-dev
Component: Code » ajax system

Changing project to core to provide a fix based on changes made for 2651102 - using checkboxes for exposed filters results in zero results displaying.

sukanya.ramakrishnan’s picture

Submitting 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

dawehner’s picture

Issue tags: +Needs tests
sukanya.ramakrishnan’s picture

Version: 9.x-dev » 8.2.x-dev
Component: ajax system » views.module
sukanya.ramakrishnan’s picture

Status: Needs work » Needs review

Version: 8.2.x-dev » 8.3.x-dev

Drupal 8.2.6 was released on February 1, 2017 and is the final full bugfix release for the Drupal 8.2.x series. Drupal 8.2.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.3.0 on April 5, 2017. (Drupal 8.3.0-alpha1 is available for testing.)

Bug reports should be targeted against the 8.3.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.4.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

mroycroft’s picture

StatusFileSize
new883 bytes

Updated patch for 8.2, there were problems introduced when there were multiple exposed checkbox filters with values set in each filter.

+++ b/core/modules/views/src/Form/ViewsExposedForm.php
@@ -181,7 +181,10 @@ public function submitForm(array &$form, FormStateInterface $form_state) {
+            $checked_array[$option_id] = $value[$option_id];

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.

mikeker’s picture

Version: 8.3.x-dev » 8.4.x-dev
StatusFileSize
new506 bytes

What 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.

mikeker’s picture

What 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! :)

mikeker’s picture

No idea why it double-posted when I was just trying to edit... Sorry about the noise.

lendude’s picture

Is 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.

mikeker’s picture

Title: Clean up user input for exposed checkboxes » Using checkboxes with an "is all of" filter fails if any options are selected
Issue summary: View changes

Is the IS still up to date?

Nope, 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.

mikeker’s picture

Issue summary: View changes
lendude’s picture

Status: Needs review » Needs work

@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.

mikeker’s picture

Status: Needs work » Needs review
StatusFileSize
new13.78 KB
new14.43 KB

Sorry 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.

mikeker’s picture

Issue tags: -Needs tests

The last submitted patch, 31: 2687773-checkboxes-tests-only.patch, failed testing. View results
- codesniffer_fixes.patch Interdiff of automated coding standards fixes only.

Status: Needs review » Needs work

The last submitted patch, 31: 2687773-checkboxes.patch, failed testing. View results
- codesniffer_fixes.patch Interdiff of automated coding standards fixes only.

mikeker’s picture

Status: Needs work » Needs review
StatusFileSize
new754 bytes
new14.43 KB

The 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.

Version: 8.4.x-dev » 8.5.x-dev

Drupal 8.4.0-alpha1 will be released the week of July 31, 2017, which means new developments and disruptive changes should now be targeted against the 8.5.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

lendude’s picture

Version: 8.5.x-dev » 8.4.x-dev
Status: Needs review » Needs work

@mikeker++

just some nits:

  1. +++ b/core/modules/views/tests/src/Functional/Plugin/ExposedFormCheckboxesTest.php
    @@ -0,0 +1,171 @@
    +  use \Drupal\field\Tests\EntityReference\EntityReferenceTestTrait;
    

    Lets not use a fully qualified name here. (See all the other uses of EntityReferenceTestTrait).

  2. +++ b/core/modules/views/tests/src/Functional/Plugin/ExposedFormCheckboxesTest.php
    @@ -0,0 +1,171 @@
    +  /**
    +   * Views used by this test.
    +   *
    +   * @var array
    +   */
    

    Can just be @inheritdoc

  3. +++ b/core/modules/views/tests/src/Functional/Plugin/ExposedFormCheckboxesTest.php
    @@ -0,0 +1,171 @@
    +  /**
    +   * Modules to enable.
    +   *
    +   * @var array
    +   */
    

    Can just be @inheritdoc

  4. +++ b/core/modules/views/tests/src/Functional/Plugin/ExposedFormCheckboxesTest.php
    @@ -0,0 +1,171 @@
    +    ViewTestData::createTestViews(get_class($this), ['views_test_config']);
    

    lets use self::class instead of get_class, it's what the cool kids do I've been told ;)

  5. +++ b/core/modules/views/tests/src/Functional/Plugin/ExposedFormCheckboxesTest.php
    @@ -0,0 +1,171 @@
    +    // Create two content types
    

    missing period at the end.

mikeker’s picture

Status: Needs work » Needs review
StatusFileSize
new1.63 KB
new14.39 KB

@Lendude, thank you for the review! Nits have been picked. :)

lendude’s picture

Status: Needs review » Reviewed & tested by the community

@mikeker thanks for pickin' them!

  • catch committed 8210ebe on 8.5.x
    Issue #2687773 by mikeker, sukanya.ramakrishnan, dawehner, amateescu,...

  • catch committed fe3a006 on 8.4.x
    Issue #2687773 by mikeker, sukanya.ramakrishnan, dawehner, amateescu,...

catch credited catch.

catch’s picture

Status: Reviewed & tested by the community » Fixed

Committed/pushed to 8.5.x and cherry-picked to 8.4.x. Thanks!

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.