Problem/Motivation

First issue:
When using a list field as an exposed grouped filter with the checkbox to remember selections, I get the following error: "An illegal choice has been detected. Please contact the site administrator.":

illegal_choice

Filter settings:

list field settings 1
list field settings 2

Second issue:
When using a Boolean field as an exposed grouped filter with the checkbox to remember selections, I get the following error: "An illegal choice has been detected. Please contact the site administrator.":

boolean settings 1
boolean settings 2

Steps to reproduce

Steps to reproduce:
- Import configs
- Add 6 nodes with filled fields (list_field_test: 2(Nodes) - Active, 2 - Inactive, 2 - Locked and test_status: 3 - Published, 3 - Unpublished)
- Go to path /test_list_view
- Select filters:
List Field Test (Inactive), Test Status (Not Published)
or
List Field Test (Inactive), Test Status (- Any -)
- Submit search
- Go to any path
- Again go to path /test_list_view

In different variations of the search query - we get an error:
An illegal choice has been detected. Please contact the site administrator.

Proposed resolution

Added a code for proper select remembered filters with grouped exposed filters

Remaining tasks

User interface changes

API changes

Data model changes

Release notes snippet

CommentFileSizeAuthor
#72 2877061-nr-bot__rnhdijs.txt90 bytesneeds-review-queue-bot
#44 illegal-choice-remember-selection-2877061-44.patch2.68 KBsumit saini
#36 illegal-choice-remember-selection-2877061-36.patch3.24 KBanacolautti
#31 interdiff-2877061-25-32.txt838 bytesakalata
#31 illegal-choice-remember-selection-2877061-31.patch3.11 KBakalata
#25 illegal-choice-remember-selection-2877061-25.patch4.09 KBtoddses
#23 illegal-choice-in-grouped-exposed-filters-with-enabled-option-remember-2877061-23.patch3.11 KBblackiceua
#17 2877061-17-store_group_input_value.patch1.14 KBandrewbelcher
#13 illegal-choice-in-grouped-exposed-filters-with-enabled-option-remember-2877061-13.patch2.98 KBblackiceua
#11 illegal-choice-in-grouped-exposed-filters-with-enabled-option-remember-2877061-11.patch2.63 KBblackiceua
#9 illegal-choice-in-grouped-exposed-filters-with-enabled-option-remember-2877061-9.patch4.01 KBblackiceua
#6 illegal-choice-in-grouped-exposed-filters-with-enabled-option-remember-2877061-6.patch3.72 KBblackiceua
#3 illegal-choice-in-grouped-exposed-filters-with-enabled-option-remember-2877061-2.patch3.62 KBblackiceua
list_field_settings_2.png61.16 KBblackiceua
boolean_settings_2.png35.59 KBblackiceua
boolean_settings_1.png55.15 KBblackiceua
list_filed_settings_1.png56.64 KBblackiceua
illegal_choice.png22.97 KBblackiceua
core.entity_form_display.node_.test_list_fields_node.default.yml1.74 KBblackiceua
core.entity_view_display.node_.test_list_fields_node.default.yml1010 bytesblackiceua
core.entity_view_display.node_.test_list_fields_node.teaser.yml588 bytesblackiceua
field.field_.node_.test_list_fields_node.body_.yml575 bytesblackiceua
field.field_.node_.test_list_fields_node.list_field_test.yml616 bytesblackiceua
field.field_.node_.test_list_fields_node.test_status.yml609 bytesblackiceua
field.storage.node_.list_field_test.yml537 bytesblackiceua
field.storage.node_.test_status.yml323 bytesblackiceua
node.type_.test_list_fields_node.yml336 bytesblackiceua
views.view_.test_list_view.yml7.75 KBblackiceua

Issue fork drupal-2877061

Command icon 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

BlacKICEUA created an issue. See original summary.

blackiceua’s picture

Issue summary: View changes
blackiceua’s picture

Proposed solution: Not the best, but working

blackiceua’s picture

Status: Active » Needs review

Status: Needs review » Needs work
blackiceua’s picture

Fixed. Applies only for BooleanOperator and ListField.

blackiceua’s picture

Status: Needs work » Needs review

Status: Needs review » Needs work
blackiceua’s picture

Added use statement for ListField class.

Status: Needs review » Needs work
blackiceua’s picture

Status: Needs work » Needs review
StatusFileSize
new2.63 KB

Added new value with prefix _group to store selected group in a Grouped Filter.

lendude’s picture

Status: Needs review » Needs work
Issue tags: -views, -exposed filter +Needs tests

@BlacKICEUA thanks for the report and the patch!

Fix could use some additional comments about what's going on. Also, this really needs some tests.

blackiceua’s picture

Status: Needs work » Needs review
StatusFileSize
new2.98 KB

Fixed issue for grouped "string" filters.

blackiceua’s picture

Status: Needs review » Needs work

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

Drupal 8.3.6 was released on August 2, 2017 and is the final full bugfix release for the Drupal 8.3.x series. Drupal 8.3.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.4.0 on October 4, 2017. (Drupal 8.4.0-alpha1 is available for testing.)

Bug reports should be targeted against the 8.4.x-dev branch from now on, and new development or disruptive changes should 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.

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

Drupal 8.4.4 was released on January 3, 2018 and is the final full bugfix release for the Drupal 8.4.x series. Drupal 8.4.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.5.0 on March 7, 2018. (Drupal 8.5.0-alpha1 is available for testing.)

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

andrewbelcher’s picture

Status: Needs work » Needs review
StatusFileSize
new1.14 KB

I think this can be simplified by just storing the group input value, rather the resulting filter value.

Still needs additional test, but marking for review to check against existing tests.

estoyausente’s picture

The #13 patch works as expected, thanks!

@andrewbelcher , sorry but your patch doesn't work. The error seems resolved (it doesn't be thrown) but the value is not saved correctly or is not showed properly, not sure. Basically, when you select an option in the view, when you move around the site and back again, the saved value is not correctly shown again.

Hi @Lendude I would like to help with the tests but I'm newbie making test, any help to start? Which type of test are necessary? Where can I look for an example?

Thanks!

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

Drupal 8.5.6 was released on August 1, 2018 and is the final bugfix release for the Drupal 8.5.x series. Drupal 8.5.x will not receive any further development aside from security fixes. Sites should prepare to update to 8.6.0 on September 5, 2018. (Drupal 8.6.0-rc1 is available for testing.)

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

nick hope’s picture

Patch #13 also applies and works so far for me in Drupal 8.7.7. Thank you very much @BlacKICEUA. I documented my issue here as a Better Exposed Filters issue.

nick hope’s picture

Version: 8.6.x-dev » 8.9.x-dev
Status: Needs review » Needs work

Patch #13 cannot be applied if the patches #25, #28 or #29 at https://www.drupal.org/project/drupal/issues/2884296 have been applied as they modify the same file and there is overlap/conflict. And it seems likely that patch #29 will be committed to Drupal 8.9.

Therefore patch #13 here will need to be updated to work after patch #29 there has been applied, or committed. I have tried but unfortunately I don't have the coding skills to sort it out. Any help would be greatly appreciated.

nick hope’s picture

The "Undefined index" patch was committed and backported to 8.8.x, so sites that use patch #13 for this issue will break if they update to 8.8.0, released next week on 4th December 2019.

blackiceua’s picture

Version: 8.9.x-dev » 8.8.x-dev
Status: Needs work » Needs review
StatusFileSize
new3.11 KB

Reroll patch for the Drupal ver. 8.8.

Status: Needs review » Needs work
toddses’s picture

Attaching a new patch here, as the patch in #23 resolves the illegal choice issue but does not actually remember the selections, particularly when using Better Exposed Filters checkboxes.

Relevant BEF issue: https://www.drupal.org/project/better_exposed_filters/issues/2935312

This effectively consolidates patch #23 from above and patch #4 in the noted issue.

toddses’s picture

I've been more deeply testing and the patch in #25 may not resolve all issues - i'm still seeing some quirks when it comes to "remembering" selections and correctly applying the saved values to the forms. Also there's the failing test which I am working to resolve.

toddses’s picture

Alright I was able to get patch #25 to work as is. However the issue with remembering selections is due to the exposed view block being cached, so I had to add a hook to remove caching on the block.

function MODULE_preprocess_block(&$variables) {
  if ($variables['plugin_id'] == 'views_exposed_filter_block:basic_cards-homepage')
    $variables['#cache']['max-age'] = 0;
}
liquidcms’s picture

Have a Workflows Field exposed filter which should be a select list but only offers a text field. Use grouped filter to change the filter widget and i then get the Illegal choice error when using filter, leaving the page, returning to the default view.

Tried patch #25 on to 8.8.4 and no longer get Illegal choice, now i get (default, Any):

Warning: array_filter() expects parameter 1 to be array, string given in Drupal\views\ViewExecutable->getExposedInput() (line 716 of core/modules/views/src/ViewExecutable.php).

plus, the filter doesn't return any results (with no errors) when applying any filter value.

Version: 8.8.x-dev » 8.9.x-dev

Drupal 8.8.7 was released on June 3, 2020 and is the final full bugfix release for the Drupal 8.8.x series. Drupal 8.8.x will not receive any further development aside from security fixes. Sites should prepare to update to Drupal 8.9.0 or Drupal 9.0.0 for ongoing support.

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

rick.kowal’s picture

I'm getting the same error as @liquidcms when i apply the patch.

akalata’s picture

Status: Needs work » Needs review
StatusFileSize
new3.11 KB
new838 bytes

In my testing, removing the changes to ViewExecutable resolves the issues noted in #28.

Status: Needs review » Needs work

The last submitted patch, 31: illegal-choice-remember-selection-2877061-31.patch, failed testing. View results

nick hope’s picture

Version: 8.9.x-dev » 9.3.x-dev

#31 is identical to #23, so I'm hiding the patch. Also increasing the Drupal version.

I am still getting problems, despite applying #23 or #25.

@toddses Re #27, are your exposed filters in their own block, using https://www.drupal.org/project/views_block_filter_block (or maybe https://www.drupal.org/project/views_exposed_filter_blocks)? Or does that code apply to a regular Views block that has exposed filters in it?

bburg’s picture

I think this issue started happening for me on a site running 9.2.6 (Or maybe this is a separate issue?). though oddly, it seemed to work fine before I was making some changes to the view. In this case, I am using a date filter, and I have the following configuration:

A date field filter
Exposed
Grouped filters enabled
"Optional disabled"
One group with the setting: Greater than or equal, Offset date, "0 minutes"
Another group with the setting: Less than, Offset date, "0 minutes"

So essentially a filter for "Upcoming" or "Past" dates

When I have the "Optional" setting disabled, I get the "An illegal choice has been detected. Please contact the site administrator." error. Which I think is happening because the view isn't respecting that I have the "Upcoming" filter option group set as the default. It instead tries to use the "- Any -" option, which shouldn't be available if there the filter is not set to be optional.

One kicker is that this is a search api view.

Like I said, I may be experiencing a completely different issue, but this seems like it could be similar...

The patch in #31 does not resolve this for me.

I'm going to say nevermind on my whole comment, because I just tested this on a different site with an all-new view, and it seems to work fine.

Version: 9.3.x-dev » 9.4.x-dev

Drupal 9.3.0-rc1 was released on November 26, 2021, which means new developments and disruptive changes should now be targeted for the 9.4.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

anacolautti’s picture

Hi! I rerolled patch #31 so that it applies to Drupal 9.3.x correctly.

I just had to apply the changes manually because the lines changed for most of the patch.

I'm not 100% sure about the last part though. I changed from this:

@@ -1416,6 +1441,7 @@ public function storeGroupInput($input, $status) {
       $session = &$_SESSION['views'][$this->view->storage->id()][$display_id];
 
       $session[$this->options['group_info']['identifier']] = $input[$this->options['group_info']['identifier']];
+      $session[$this->options['group_info']['identifier'] . '_group'] = $this->group_info;
     }
   }

to this:

@@ -1464,6 +1489,7 @@ public function storeGroupInput($input, $status) {
         $views_session[$this->view->storage->id()][$display_id] = [];
       }
       $views_session[$this->view->storage->id()][$display_id][$this->options['group_info']['identifier']] = $input[$this->options['group_info']['identifier']];
+      $views_session[$this->view->storage->id()][$display_id][$this->options['group_info']['identifier'] . '_group'] = $this->group_info;
     }
     if (!empty($views_session)) {
       $session->set('views', $views_session);

If someone with more understanding could review this, it would be great. Thanks.

bramdriesen’s picture

Following.

lind101 made their first commit to this issue’s fork.

Version: 9.4.x-dev » 9.5.x-dev

Drupal 9.4.0-alpha1 was released on May 6, 2022, which means new developments and disruptive changes should now be targeted for the 9.5.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.5.x-dev » 10.1.x-dev

Drupal 9.5.0-beta2 and Drupal 10.0.0-beta2 were released on September 29, 2022, which means new developments and disruptive changes should now be targeted for the 10.1.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

quietone’s picture

The related issue has been fixed. so this needs to be re-tested to confirm if is it still a problem.

The next step here is to confirm that this is still a problem. I am adding the tag for manual testing and updating the Issue Summary to add that as a remaining task.

Cheers

Version: 10.1.x-dev » 11.x-dev

Drupal core is moving towards using a “main” branch. As an interim step, a new 11.x branch has been opened, as Drupal.org infrastructure cannot currently fully support a branch named main. New developments and disruptive changes should now be targeted for the 11.x branch, which currently accepts only minor-version allowed changes. For more information, see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

sumit saini’s picture

The issue is still present for grouped filters on D9.5.9. So, here is a reroll of the patch in #36.

david.qdoscc’s picture

Status: Needs work » Reviewed & tested by the community

I can confirm that this is still an issue as of D10.1.7. Patch #44 applies cleanly and resolves the issue for us.

needs-review-queue-bot’s picture

Status: Reviewed & tested by the community » Needs work

The Needs Review Queue Bot tested this issue.

While you are making the above changes, we recommend that you convert this patch to a merge request. Merge requests are preferred over patches. Be sure to hide the old patch files as well. (Converting an issue to a merge request without other contributions to the issue will not receive credit.)

rainer f. gottlieb’s picture

Version: 11.x-dev » 9.5.x-dev

It does not seem to work for role filter grouping? With 9.5 patch #36 #44 is applied but the error as described above remains, at least in our case. Maybe someone can take another look at this case.

bramdriesen’s picture

Version: 9.5.x-dev » 11.x-dev

Standard procedure is to fix this on the 11.x branch and then backport to other release branches.

ivnish made their first commit to this issue’s fork.

ivnish changed the visibility of the branch 10.0.x to hidden.

ivnish changed the visibility of the branch 11.x to hidden.

ivnish changed the visibility of the branch 2877061-illegal-choice-remembered-group-filters to hidden.

ivnish’s picture

Status: Needs work » Needs review
Issue tags: -Needs manual testing

I rerolled issue fork to 11.x and created MR

I also use this patch in my project and it fixed bug from issue title

smustgrave’s picture

Status: Needs review » Needs work

Still appears to need test coverage

Also can a proposed solution be added to the issue summary please

Thanks!

ivnish’s picture

Status: Needs work » Needs review
Issue tags: -Needs tests

@smustgrave

Still appears to need test coverage

I added a test

Also can a proposed solution be added to the issue summary please

I don't know how to name it. "Added some code to fix the bug"? :)

smustgrave’s picture

Status: Needs review » Needs work

The summary is for the committer and reviewer(s).

Looking at hte code see there is a new check what's it checking? This may help write one https://www.drupal.org/docs/develop/issues/fields-and-other-parts-of-an-...

Not adding one will most likely delay the issue until someone wants to get the information the long way.

smustgrave’s picture

Status: Needs work » Needs review

Will keep in review though

ivnish’s picture

Issue summary: View changes
ivnish’s picture

Assigned: Unassigned » ivnish
krakenbite’s picture

Status: Needs review » Reviewed & tested by the community

MR works fine. So I changed status to RTBC

ivnish’s picture

Assigned: ivnish » Unassigned
ankitv18’s picture

Status: Reviewed & tested by the community » Needs work

All I have noticed is that multiple if-else condition with several & operators to check
I think it should be more readable and also there are few suggestions already done on the MR by community, hence moving into NW

ivnish’s picture

Status: Needs work » Needs review
krakenbite’s picture

Status: Needs review » Reviewed & tested by the community

MR works fine. So I changed status to RTBC

bramdriesen’s picture

Not entirely sure #63 was handled. There is still quite a lot of if elseif else nesting being added here which increases the code complexity/readability.

catch’s picture

Status: Reviewed & tested by the community » Needs work

Yeah I wonder if there's a way to simplify per #67, left a couple of comments on the MR.

gregn0r’s picture

It doesn't quite work for me

I have an on/off checkbox filter using BEF and with AJAX enabled

Using the MR it does remember/save the selection - if the box is not checked, I can check it and then reload the page and it stays checked, however if I uncheck the box it does the ajax request and then re-checks the box - I am then unable to change the state once its been saved from being originally checked

There is no other JS interfering with this button

ivnish’s picture

Status: Needs work » Needs review

I improved some code. Please review

bramdriesen’s picture

Status: Needs review » Reviewed & tested by the community

Looks a lot better and readable. The test only run fails as expected.

needs-review-queue-bot’s picture

Status: Reviewed & tested by the community » Needs work
StatusFileSize
new90 bytes

The Needs Review Queue Bot tested this issue. It no longer applies to Drupal core. Therefore, this issue status is now "Needs work".

This does not mean that the patch necessarily needs to be re-rolled or the MR rebased. Read the Issue Summary, the issue tags and the latest discussion here to determine what needs to be done.

Consult the Drupal Contributor Guide to find step-by-step guides for working with issues.

ivnish’s picture

Status: Needs work » Reviewed & tested by the community
catch’s picture

Status: Reviewed & tested by the community » Needs work

I think we need some additional code comments here to explain what's happening - left a comment on the MR in the place that's still a bit confusing for me.

plopesc’s picture

Tested the patch and worked for us. Thank you!

However, we found out some issues that seems unrelated to this issue, though.

For reference, we are facing the issue described in #2742273: Views with "remembered" filters are missing the use cache context for views where cache is enabled and remember is selected.

The view was cached with a specific filter value and selecting a new element does not clear the cache. When the page is reloaded, the original remember value is set for the filter instead of the new one.

Version: 11.x-dev » main

Drupal core is now using the main branch as the primary development branch. New developments and disruptive changes should now be targeted to the main branch.

Read more in the announcement.