Problem/Motivation

Trying to use boolean fields as exposed filters in AJAX enabled views has broken default values if you us the reset button.

- Create a view using an exposed filter for a boolean field added to a node, make it exposed and enable a reset button
- Set a default value of True for the exposed filter, uncheck 'required' (so 'All' option is added)
- View the view page , the value will be 'True'
- Press reset button, the value will be reset to - Any -

Proposed resolution

Fix it

Remaining tasks

Tests

User interface changes

API changes

Data model changes

Comments

damiankloip created an issue. See original summary.

damiankloip’s picture

Status: Active » Needs review
StatusFileSize
new2.27 KB

Status: Needs review » Needs work

The last submitted patch, 2: d8.views-exposed-bool-defaults.patch, failed testing.

dawehner’s picture

Issue tags: +JavaScriptTest

This looks perfect!

damiankloip’s picture

StatusFileSize
new2.16 KB
damiankloip’s picture

Title: boolean exposed filter default values do not work correctly with AJAX enabled views » Boolean exposed filter default values do not work correctly with AJAX enabled views
damiankloip’s picture

damiankloip’s picture

Status: Needs work » Needs review
jibran’s picture

+++ b/core/modules/views/src/Plugin/views/filter/BooleanOperator.php
@@ -157,17 +157,25 @@ protected function valueForm(&$form, FormStateInterface $form_state) {
+    $value = $this->value;
...
+    if ($value !== 'All') {
+      $value = (int) $value;
+    }

How about $value = $this->value !== 'All' ? (int) $vlaue : 'All'; this for simplicity?

mayurjadhav’s picture

Assigned: Unassigned » mayurjadhav
Status: Needs review » Needs work

Hi Agreed with you, Assigning to me.

mayurjadhav’s picture

StatusFileSize
new2.13 KB

Used ternary Operations and uploaded new patch.

mayurjadhav’s picture

Assigned: mayurjadhav » Unassigned
Status: Needs work » Needs review

Status: Needs review » Needs work

The last submitted patch, 11: 2732103-11.patch, failed testing.

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

Drupal 8.2.0-beta1 was released on August 3, 2016, which means new developments and disruptive changes should now be targeted against the 8.3.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

damiankloip’s picture

IMO, the original way is simpler and far more readable. Less lines != simplicity. So I would say let's go with the patch in #5 please :) Also the patch in #11 just copied the variable typo from #9 anyway.

@lendude, this was more a problem due to https://www.drupal.org/node/2732111, which is fixed, but I think it makes sense to do this anyway.

lendude’s picture

@damiankloip Seems like #2732111: Reset button doesn't work and never gets removed on AJAX enabled views fixed it indeed, I can't reproduce this anymore using the steps in the IS. So do you think we should still make these changes more as hardening then an actual bug fix?
So change this from bug to task?
Or just leave it as is and close this?

damiankloip’s picture

Category: Bug report » Task

Yeah, that issues basically fixed all the related issues, but just kind of sidesteps this issue. I think we make this a task now and still try to get it in.

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

Drupal 8.3.0-alpha1 will be released the week of January 30, 2017, which means new developments and disruptive changes should now 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.

mkalkbrenner’s picture

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

re-rolled against 8.4.x and fixed the failure in #11.

damiankloip’s picture

Good to see this moving again! can we revert the ternary changes as per #16 please?

mkalkbrenner’s picture

The error seems to exist in multiple layers. To get my setup to work I had to fix #2712131: D8: Views: UI: -Any- option for non-required boolean yes/no filter won't stick as well.

And from point of view we have a real bug here, not a task. And it's not just limited to Ajax.

sk33lz’s picture

Status: Needs review » Reviewed & tested by the community
Issue tags: +Baltimore2017

The patch from #20 applies cleanly to 8.4.x and creating a view as per the OP's description keeps the proper exposed filter settings.

This should be RTBC.

tstoeckler’s picture

Status: Reviewed & tested by the community » Needs work

This should still be updated per #21 / #16.

Also:

+++ b/core/modules/views/src/Plugin/views/filter/BooleanOperator.php
@@ -162,17 +162,20 @@ protected function valueForm(&$form, FormStateInterface $form_state) {
+    // We can cast to an int here as if 'All' is used, it will be set from
+    // the user input.

This comment doesn't really make sense to me. It seems we are casting to int unless the value is 'All', but the comment makes it seem like it's the other way around.

damiankloip’s picture

Yes, that was my bad in the original patch.. Should be "Cast to an int unless 'All' is used.." I think.

mkalkbrenner’s picture

Status: Needs work » Needs review
StatusFileSize
new882 bytes
new2.18 KB

I prefer the ternary operator. But I don't won't to block the solution here.

Status: Needs review » Needs work

The last submitted patch, 26: 2732103-25.patch, failed testing.

mkalkbrenner’s picture

Status: Needs work » Needs review

CI caused the fail. Retest succeeded.

tstoeckler’s picture

Assigned: Unassigned » tstoeckler
Status: Needs review » Needs work
Issue tags: +Needs tests

I think this could use a test. I also don't 100% grok this issue yet, so I will use writing a test as an opportunity to fix that.

tstoeckler’s picture

Status: Needs work » Needs review
Issue tags: -Needs tests
StatusFileSize
new5.21 KB

Hmm... so I followed the steps to reproduce in the issue summary but I couldn't reproduce the issue. The attached patch is a test that follows the steps to reproduce, as well. And it passes, instead of failing. Note that the title mentions "AJAX-enabled", but I couldn't reproduce this behavior with or without AJAX, so the patch currently leaves that out.

Marking "needs review", but this is really "needs work" until we get a failing "tests-only" patch.

tstoeckler’s picture

Status: Needs review » Needs work
tstoeckler’s picture

Assigned: tstoeckler » Unassigned
lendude’s picture

@tstoeckler I think the actual issue was solved by #2732111: Reset button doesn't work and never gets removed on AJAX enabled views and the issue @mkalkbrenner ran into is solved by #1381140: A #default_value = 0 for #type radios checks all radios so I think this can be reset to a Task again, just for internal hardening of the used values, see #17/#18

tstoeckler’s picture

Hmm... OK. Tagging "Needs issue summary update" then. It would be great to clarify what is being "hardened" here.

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.

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

Drupal 8.5.0-alpha1 will be released the week of January 17, 2018, which means new developments and disruptive changes should now 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.

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

Drupal 8.6.0-alpha1 will be released the week of July 16, 2018, which means new developments and disruptive changes should now 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.

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

Drupal 8.7.0-alpha1 will be released the week of March 11, 2019, which means new developments and disruptive changes should now be targeted against the 8.8.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.8.x-dev » 8.9.x-dev

Drupal 8.8.0-alpha1 will be released the week of October 14th, 2019, which means new developments and disruptive changes should now be targeted against the 8.9.x-dev branch. (Any changes to 8.9.x will also be committed to 9.0.x in preparation for Drupal 9’s release, but some changes like significant feature additions will be deferred to 9.1.x.). For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

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

Drupal 8.9.0-beta1 was released on March 20, 2020. 8.9.x is the final, long-term support (LTS) minor release of Drupal 8, which means new developments and disruptive changes should now 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.

Version: 9.1.x-dev » 9.2.x-dev

Drupal 9.1.0-alpha1 will be released the week of October 19, 2020, which means new developments and disruptive changes should now be targeted for the 9.2.x-dev branch. For more information see the Drupal 9 minor version schedule and the Allowed changes during the Drupal 9 release cycle.

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

Drupal 9.2.0-alpha1 will be released the week of May 3, 2021, which means new developments and disruptive changes should now be targeted for the 9.3.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.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.

andypost’s picture

+++ b/core/modules/views/src/Plugin/views/filter/FilterPluginBase.php
@@ -1190,7 +1190,9 @@ protected function exposedTranslate(&$form, $type) {
-      $form['#default_value'] = 'All';
+      if (!isset($this->value)) {
+        $form['#default_value'] = 'All';

it needs code comment - why default value should "All" when no value?

andypost’s picture

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

Status: Needs work » Closed (outdated)
Issue tags: +Bug Smash Initiative

I tested this on Drupal 10.1.x, umami install. I added a boolean to a new content type and followed the steps in the Issue Summary. I was not able to reproduce the problem. After filtering on 'any', 'true' or 'false' and then clicking 'reset' the filter was set to the default value of True.

Therefor, I am closing this as outdated.

Thanks.