Views is not remembering default values for exposed filters. Steps to reproduce:

1. Create a view to list nodes.
2. Create an exposed filter on the Published field, make it non required and select a default value such as "No"
3. Save the view and go to the view's page.
4. The default value is not working.
5. Go back to editing the view and open the filter settings, the defualt value is gone (was probably never saved).

Displaying the right selected value is blocked by #1381140: A #default_value = 0 for #type radios checks all radios.

Because the boolean filter stores it value as a boolean the 'All' value can never be saved. It gets cast to TRUE. To facilitate the storing of the 'All' option, the boolean filter needs to store its values as a string.

CommentFileSizeAuthor
#64 boolean_default-40083.patch2.49 KBlarowlan
#54 boolean_default-2459289-52.patch62.59 KBLendude
#54 interdiff-2459289-47-52.txt448 bytesLendude
#47 interdiff-2459289-45-47.txt2.58 KBLendude
#47 boolean_default-2459289-47.patch61.95 KBLendude
#45 boolean_default-2459289-45.patch59.24 KBLendude
#45 interdiff-2459289-42-45.txt34.74 KBLendude
#42 boolean_default-2459289-42.patch57.99 KBLendude
#42 interdiff-2459289-39-42.txt743 bytesLendude
#39 boolean_default-2459289-39.patch57.99 KBLendude
#39 interdiff-2459289-32-39.txt21.15 KBLendude
#32 boolean_default-2459289-32.patch38.93 KBLendude
#32 interdiff-2459289-30-32.txt10.83 KBLendude
#30 boolean_default-2459289-30.patch23.52 KBLendude
#30 interdiff-2459289-28-30.txt1.67 KBLendude
#28 boolean_default-2459289-28.patch23.42 KBLendude
#28 boolean_default-2459289-28-TEST_ONLY.patch1.11 KBLendude
#23 drupal-2459289-23.patch1.11 KBkalistos
#20 core_view_fix_issue_v3.diff1.54 KBdhrjgpt2005@gmail.com
#18 core_view_fix_issue_v2.diff1.51 KBdhrjgpt2005@gmail.com
#15 core_view_diff_2459289_v1.diff1.56 KBdhrjgpt2005@gmail.com
#11 core_view_diff_2459289_updated.diff1.56 KBdhrjgpt2005@gmail.com
#8 core_view_diff_2459289.diff1.72 KBdhrjgpt2005@gmail.com
Support from Acquia helps fund testing for Drupal Acquia logo

Comments

Lendude’s picture

Title: Default values are not saved » Boolean default values are not saved
Issue tags: +VDC

The only value that seem to have a problem are boolean filters. Other filters that I tested seemed to work fine.

For the boolean values there are a couple of different things happening:
- Yes (value 1) works
- No (value 0) doesn't work. It's not saved but does shows up selected after clicking Apply and then opening the filter edit screen again. Opening the filter edit screen after save shows no selected radios.
- - Any - (value All) doesn't work but this gets interesting! When you select -Any- and apply and then open the filter settings again 'No' is selected (because of #1381140: A #default_value = 0 for #type radios checks all radios), and if you select -Any- apply this and save the view and then open the filter settings 'Yes' is selected! (I'm assuming this is because the value 'all' gets cast to 1 on save).

Version: 8.0.x-dev » 8.1.x-dev

Drupal 8.0.6 was released on April 6 and is the final bugfix release for the Drupal 8.0.x series. Drupal 8.0.x will not receive any further development aside from security fixes. Drupal 8.1.0-rc1 is now available and sites should prepare to update to 8.1.0.

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

m.stenta’s picture

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

I'm experiencing this too. Moving to 8.2.x.

Lendude’s picture

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

This is a bug fix so it can stay in 8.1.x for now.

Some of the findings we got in #2369119: Fatal error when trying to save a View with grouped filters using other than string values about this.

The problem seems to be that the exposed boolean filter value cannot be stored in a boolean field, this is because it can have 3 values (yes-no-all). The non-exposed boolean filter value only has 2 values, so that's fine.

Currently there is no way to use a different format for the exposed filter value then for the non-exposed filter value.

Two things I can think of to solve this:
1-Make it possible to set a different storage format in config for the exposed and non-exposed values.
2-Make the boolean filter alway store the filter value as a string

any other solutions?

dhrjgpt2005@gmail.com’s picture

Assigned: Unassigned » dhrjgpt2005@gmail.com

Hi,

I am looking into this. I am able to reproduce it on my local setup.

dhrjgpt2005@gmail.com’s picture

@lendude : it seems to be the different issue, as the filters present on the exposed form is having 3 values while at the view preview page its showing 2 values. I think while making the filter exposed its forming 2 values instead of 3. is it the correct understanding?

dhrjgpt2005@gmail.com’s picture

@lendude : my bad, i think its could solve the issue as suggested by you i will try to modify the scripts and let you know about this.

dhrjgpt2005@gmail.com’s picture

stBorchert’s picture

Status: Active » Needs review

Changing status.

Status: Needs review » Needs work

The last submitted patch, 8: core_view_diff_2459289.diff, failed testing.

dhrjgpt2005@gmail.com’s picture

dhrjgpt2005@gmail.com’s picture

Status: Needs work » Needs review

Status: Needs review » Needs work

The last submitted patch, 11: core_view_diff_2459289_updated.diff, failed testing.

dhrjgpt2005@gmail.com’s picture

Status: Needs work » Needs review
dhrjgpt2005@gmail.com’s picture

Status: Needs review » Needs work

The last submitted patch, 15: core_view_diff_2459289_v1.diff, failed testing.

dhrjgpt2005@gmail.com’s picture

Status: Needs work » Needs review
dhrjgpt2005@gmail.com’s picture

Status: Needs review » Needs work

The last submitted patch, 18: core_view_fix_issue_v2.diff, failed testing.

dhrjgpt2005@gmail.com’s picture

Status: Needs review » Needs work

The last submitted patch, 20: core_view_fix_issue_v3.diff, failed testing.

andrewbelcher’s picture

#2734197: FilterPluginBase::acceptExposedInput should check 'All' strictly has been postponed on this as @Lendude thinks this is the root cause, though from my testing I am not sure that is the case.

kalistos’s picture

dhrjgpt2005, did you mean this one?

dhrjgpt2005@gmail.com’s picture

Hi Kalistos,

Thanks for the perfect patch.
Need to run this patch with 8.1 and 8.2 also needs to update some of the test cases.

I will be doing the fixes for the test cases soon.

dhrjgpt2005@gmail.com’s picture

Status: Needs work » Needs review

Status: Needs review » Needs work

The last submitted patch, 23: drupal-2459289-23.patch, failed testing.

HnLn’s picture

Is there any workaround for the moment ? If I do a form alter on views_exposed_form and set the default value to 'All', the default value still jumps to the first option (true). Tried setting it to to '- Any -' as well.

Lendude’s picture

Thanks for taking a look at this @dhrjgpt2005 but not sure if #23 is the way to go.

Here is my first stab at a fix. This converts the values for boolean filters to strings.

Couple of things:

Status: Needs review » Needs work

The last submitted patch, 28: boolean_default-2459289-28.patch, failed testing.

Lendude’s picture

Status: Needs work » Needs review
FileSize
1.67 KB
23.52 KB

This should clear up some fails.

Status: Needs review » Needs work

The last submitted patch, 30: boolean_default-2459289-30.patch, failed testing.

Lendude’s picture

Lets see if I got them all. Passes locally now.

dhrjgpt2005@gmail.com’s picture

@lendude thanks for looking into this. I will verify this at my end too.

dhrjgpt2005@gmail.com’s picture

Assigned: Unassigned » dhrjgpt2005@gmail.com
Status: Needs review » Reviewed & tested by the community
dhrjgpt2005@gmail.com’s picture

Assigned: dhrjgpt2005@gmail.com » Unassigned
Status: Reviewed & tested by the community » Needs review
dhrjgpt2005@gmail.com’s picture

@lendude: i tried to run the patch on 5.6 and it gets failed on 2 locations, so do we need to look into that as well.

dawehner’s picture

I would have expected that the boolean filter itself would have to be changed, now that we are using strings, instead of booleans. This might be a reason for the failure in 5.6?

  1. +++ b/core/modules/views/views.install
    @@ -390,5 +390,39 @@ function views_update_8101() {
    + * Set all boolean filter values to strings.
    + */
    +function views_update_8102() {
    

    Is there a reason we cannot use a post update here?

  2. +++ b/core/modules/views/views.install
    @@ -390,5 +390,39 @@ function views_update_8101() {
    +            $new_value = FALSE;
    +            // Update all boolean values to strings, ignore other values.
    +            if ($filter['value'] === TRUE) {
    +              $new_value = '1';
    +            }
    +            elseif ($filter['value'] === FALSE) {
    +              $new_value = '0';
    +            }
    

    There might be instances with 1/0 out there, should we take into account them as well?

dawehner’s picture

Status: Needs review » Needs work
Lendude’s picture

I would have expected that the boolean filter itself would have to be changed

So would I, but the code that distinguishes between on and off in \Drupal\views\Plugin\views\filter\BooleanOperator::queryOpBoolean is if (empty($this->value)) {, so it doesn't matter is you give it FALSE, 0 or "0", it's all empty. It just works.

37.1 Only reason was that I had no idea that existed, fixed.
37.2 Updated, it would be a bit weird to run into that because that would violate the current schema too, but who knows what's floating around out there.

Wrote a update test, but can't get it to pass locally, keeps timing out, so its completely untested so I don't have high hopes for it yet.

No idea yet why it fails on 5.6.

Status: Needs review » Needs work

The last submitted patch, 39: boolean_default-2459289-39.patch, failed testing.

The last submitted patch, 39: boolean_default-2459289-39.patch, failed testing.

Lendude’s picture

Version: 8.1.x-dev » 8.2.x-dev
FileSize
743 bytes
57.99 KB

This is probably to invasive to target 8.1.x, so moving to 8.2.x

lets see what this fixes.

Lendude’s picture

Status: Needs work » Needs review

Status: Needs review » Needs work

The last submitted patch, 42: boolean_default-2459289-42.patch, failed testing.

Lendude’s picture

Status: Needs work » Needs review
FileSize
34.74 KB
59.24 KB

Update test passes locally now and some more views got added.

Moved the upgrade test view to fixtures folder so it won't cause a fail of TestViewsTest (since the whole point of the view is that it starts invalid). Other option would be to change TestViewsTest to allow a view to be ignored.

This patch will fail every time a new (test)view is committed that uses the status filter.

Status: Needs review » Needs work

The last submitted patch, 45: boolean_default-2459289-45.patch, failed testing.

Lendude’s picture

The failing test was because \Drupal\system\Tests\Update\UpdatePathTestBase::runUpdates assumes that the config is up-to-date even when you have (and expected) failing updates. This seems strange to me, so I moved the config check into the if ($this->checkFailedUpdates) so you only expect an up-to-date config when you expect no failing tests.

dawehner’s picture

Status: Needs review » Reviewed & tested by the community

The failing test was because \Drupal\system\Tests\Update\UpdatePathTestBase::runUpdates assumes that the config is up-to-date even when you have (and expected) failing updates. This seems strange to me, so I moved the config check into the if ($this->checkFailedUpdates) so you only expect an up-to-date config when you expect no failing tests.

That is totally logical

Status: Reviewed & tested by the community » Needs work

The last submitted patch, 47: boolean_default-2459289-47.patch, failed testing.

The last submitted patch, 47: boolean_default-2459289-47.patch, failed testing.

The last submitted patch, 47: boolean_default-2459289-47.patch, failed testing.

The last submitted patch, 47: boolean_default-2459289-47.patch, failed testing.

The last submitted patch, 47: boolean_default-2459289-47.patch, failed testing.

Lendude’s picture

A new test view got committed so this was failing again.

Updated the new view.

This is going to be the reroll patch from hell :)

dawehner’s picture

Status: Needs review » Reviewed & tested by the community

A new test view got committed so this was failing again.

That is always a good thing

Status: Reviewed & tested by the community » Needs work

The last submitted patch, 54: boolean_default-2459289-52.patch, failed testing.

Lendude’s picture

Status: Needs work » Reviewed & tested by the community

Unrelated fails, the fail on php 5.6 looks unrelated too.

catch’s picture

Status: Reviewed & tested by the community » Needs review

What happens if a contrib module provides a view as default config using this filter? I see the test fails above with test views, but not clear if that translates to an actual user-facing/config install issue with an exported view.

dawehner’s picture

Status: Needs review » Reviewed & tested by the community

This is not a concern here, because we don't change any runtime code. If something magically managed to save the right value, for example by manual changing things, they continue to work.

catch’s picture

Status: Reviewed & tested by the community » Fixed

Makes sense. Committed/pushed to 8.3.x and cherry-picked to 8.2.x. Thanks!

  • catch committed 6c614cb on 8.3.x
    Issue #2459289 by Lendude, dhrjgpt2005, kalistos, dawehner, david_garcia...
dhrjgpt2005@gmail.com’s picture

dhrjgpt2005@gmail.com’s picture

larowlan’s picture

FileSize
2.49 KB

8.1 reroll for those who're interested, credit to @welly

Status: Fixed » Closed (fixed)

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