Problem/Motivation
The feature "Remember the last selection" for views exposed filters doesn't seem to work anymore.
This regressed in #2473875: Convert uses of $_SESSION to symfony session retrieved from the request when we updated our session handling. (See #9 for the research from git blame).
Steps to reproduce
- Create a views page and add one or more exposed filters. Check the option "Remember the last selection" and select user roles "anonymous" and "authenticated"
- Under Advanced / Use Ajax select "Yes"
- Under Advanced / Caching select "none"
- Go to the created views page and make a selection on the exposed filter
- Reload the page
- The previous selection isn't remembered, the view is shown in default state
This used to work in previous drupal versions.
Proposed resolution
The solution is to update the code to use the Symfony session instead of $_SESSION. (See #15.)
| Comment | File | Size | Author |
|---|
Issue fork drupal-3260652
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
Comment #2
cilefen commentedCan you identify the Drupal release that caused the regression?
Comment #3
cilefen commentedI think I can reproduce this but I am not a Views expert. It will be really useful to determine what release introduced this.
Comment #4
finallylea commentedComment #5
finallylea commentedI've just tested this on a few clean installs. The issue seems to be introduced by release 9.3 (9.2.11 works fine, 9.3.0 failed).
Comment #6
cilefen commentedThe fastest way to pin this down would be to perform a git bisect on 9.2.11 vs 9.3.0.
Comment #7
finallylea commentedDrupal 9.3.x doesn't seem to store the actual filer value in the $_SESSION variable.

Comment #8
cilefen commentedI figured that was one of the ways this could break. So we just need to find the commit that did this by checking the release notes, looking at the commit log, diffing 9.2.11 and 9.3.0 (and then git blame), or git bisect.
Comment #9
finallylea commentedI've just found the commit with git bisect:
[492b7a181da3d074beb2e62f7e69ffc27f79d7f6] Issue #2473875 by znerol, alexpott, andypost, kim.pepper, amit.drupal, raman.b, martin107, joachim: Convert uses of $_SESSION to symfony session retrieved from the requestComment #10
cilefen commentedMagnificent. I will comment on that issue to alert them to this one.
Comment #11
cilefen commentedComment #12
finallylea commentedThank you!
Comment #13
finallylea commentedComment #14
andypostComment #15
peleccotur commentedWe recently ran into this issue as well. I checked the views module and only noticed 2 lines using $_SESSION. After updating those to use the Symfony session, remembering the filters works again.
I included a patch file containing the fix.
Comment #16
cilefen commentedComment #18
finallylea commentedThank you for sharing @maarten_plessius! Works great for me.
Comment #19
stuchl4n3k commented#15 works for me in Drupal 9.3.9. Thanks Maarten!
Comment #22
bserem commentedComment #23
bserem commentedComment #24
znerol commentedThanks @maarten_plessius and @bserem for working on this. In order to make the tests pass, please wrap the whole code block into the following condition:
Comment #25
lind101 commentedTidied up the Request and Session checks as suggested in #24.
lind101 made their first commit to this issue’s fork.
Comment #26
lind101 commentedComment #28
znerol commentedThanks @lind101 for the work. It seems to me that the merge request now contains a couple of unrelated hunks. Would you mind minimizing the changes? The result should look like the patch from #15 but with an additional check around the new code as pointed out in #24.
Comment #29
lind101 commentedThanks for the feedback @znerol. The extra chunks are just me repositioning the conditionals to make the code a bit more readable and then wrapping all of the exposed input logic in a condition checking for the existance of a request (which we can't do anything without). Same code, just tidied up a bit!
If that explination doesn't suffice, just let me know and I'll dial it back as requested. :)
Cheers!
Comment #30
znerol commentedDrupal::hasRequest()is only necessary in very few edge cases. In fact grepping through the10.0.xbranch today turns up only two occurrences:I'd be very surprised if it would be necessary to add it here.
In order to make the job of the core committers easier, it is advisable to reduce the changes to the minimum which is necessary to fix a bug. This will increase the chance that a patch actually makes it into core in a timely manner.
Comment #31
znerol commentedSetting to needs work for the reason stated above.
Comment #32
lind101 commentedDone!
I've left in the change of moving the $display_id variable declaration inside the Session conditional.
Comment #33
znerol commentedThat makes sense. Thanks a lot @lind101.
Comment #34
bserem commentedRTBC +1
Comment #35
jfurnas commentedRTBC +1 Works like a dream.
Comment #36
bserem commentedForgive me if I'm wrong here, but seeing how this worked before and got broken, and given that 9.4 has not been released yet, I believe we can target 9.4 and not wait for an absurd amount of time to fix a regression. Right?
Comment #37
alexpottThanks for filing this bug report and for fixing the issue. Bug fixing is very valuable. In order to commit a bug fix, we need an automated test to prove that we've fixed the bug and ensure that we don't break it again in the future. For more information about writing tests in Drupal, see the following links:
This is especially true on this bug as we broke this unknowingly in #2473875: Convert uses of $_SESSION to symfony session retrieved from the request
Comment #38
fool2 commentedSurprising there was not a test for this in the first place! +1 on the patch resolving it for embedded views
Comment #39
tomsaw commented#15 Works. Thank you!
Comment #40
daniels____ commented+1
Comment #42
ravi.shankar commentedGiven a try to fix unresolved threads of MR, please review. Still needs work for tests.
Comment #43
jonmcl commentedNot directly related, but it appears that caching of the view will also cause problems with the "remember last selection": #2742273: Views with "remembered" filters are missing the use cache context
Comment #44
problue solutionsI have applied the patch in #15 and the selection is remembered in terms of the results of the view, but the actual value in the exposed filter form resets to the default (or the first in the list if no default is set), so the value in the excposed filter form is incorrect and is not what the view is actually displaying.
Comment #45
jfurnas commentedPatch #15 doesn't appear to work anymore. Just applied it to 9.4.9 and the exposed filters do not save.
Comment #46
znerol commentedInteresting. This is working fine here with merge request MR 2102 rebased on a fresh 9.4.x branch. Repro:
Comment #47
znerol commentedThis issue still needs tests. A good starting point is ./core/modules/views/tests/src/Functional/Plugin/ExposedFormTest.php.
I will help with advice and reviews if anybody wants to take up the task.
Comment #48
jfurnas commentedYeah, MR 2102 works, but the patch on #15 doesn't when applied. I ended up manually doing the work that MR 2102 does locally to get it working. Now just trying to get it into a localized patch until it can get fixed in Drupal core since this we have an emergent need to use this.
Comment #50
davisbenI took a shot at adding a simple test. I can add more to it if there are more cases it should cover.
Comment #51
znerol commentedThanks @davisben, the test provides coverage for the bug. I did verify locally that the test fails if the fix from MR 2102 isn't applied.
./vendor/bin/phpunit --filter=testRememberSelected core/modules/views/tests/src/Functional/Plugin/ExposedFormTest.phpCommitter feedback from #37 was addressed in #41 and a test was added in #49, thus all green IMHO.
Comment #52
znerol commented@jfurnas: There is a way to retrieve a diff from a gitlab merge request. Just add
.diffto the end of the MR URL. E.g.:https://git.drupalcode.org/project/drupal/-/merge_requests/2102.diff
Also note that it is also possible to add
.patchto the end of the URL. In that case the response contains the equivalent ofgit format-patch. E.g.:https://git.drupalcode.org/project/drupal/-/merge_requests/2102.patch
Comment #53
quietone commentedNice to see this working and with a test! It now just needs to be on 10.1.x, where the fix will be applied first.
Thanks!
Comment #54
znerol commentedIf I'm not completely mistaken, fixing regressions is allowed within the release cycle: https://www.drupal.org/about/core/policies/core-change-policies/allowed-...
Comment #55
bserem commented@quietone this is a regression that has been bugging people for a long time. I don't feel we should move it two versions in the future.
Given that 9.5.x/10.0.x are due tomorrow, it is ok we target that. But 10.1.x is too far away.
Comment #56
znerol commentedThis issue needs a rebase. There is a chapter in the issue forks docs about that process.
I will help with advice and reviews if anybody wants to take up the task.
Comment #60
rpayanmComment #61
znerol commentedThank you @rpayanm. I did a diff between MR 2102 and the new MR 3132. I did not notice any change in functionality.
Also took the new MR to a manual test-drive on a fresh 9.5.x, works beautifully. MR 3132 is good to go.
Comment #62
xjm@bserem, @quietone is a Drupal core release manager, which means she has final decision-making authority over which branches will receive a change and all other issue metadata. Therefore, when she makes a decision on the branch setting of the issue, please don't revert it.
Furthermore, regardless of the fact that this change may be eligible for backport to the production 10.0.x and 9.5.x branches as a non-disruptive bugfix, the issue must still be committed to 10.1.x first under the core backport policy. Therefore, a patch that applies to 10.1.x or a version of the MR that has 10.1.x as its base branch is still required before we can commit it to 10.0.x or 9.5.x.
Finally, due to limitations of the GitLab integration, it can be difficult to get merge requests against multiple base branches to be tested properly, and currently the easiest way to get the respective MRs to be tested properly against the correct branch is by changing the version setting on the issue. So, @quietone was doing a helpful thing in that regard as well.
Comment #65
xjmI closed the outdated merge request against 9.4.x (as well as one that had what looked like a bad merge), and hid the 9.3.x patch.
We still need a 10.1.x merge request here to proceed. I've left the passing 9.5.x merge request open as that will still be useful when it comes time to commit the backport (because I agree this issue is probably backportable as a non-disruptive bugfix).
Thanks!
Comment #68
rpayanmI created a patch for 10.0.x and 10.1.x branches, please review.
Comment #69
smustgrave commentedThis issue is being reviewed by the kind folks in Slack, #need-reveiw-queue. We are working to keep the size of Needs Review queue [2700+ issues] to around 400 (1 month or less), following Review a patch or merge require as a guide.
Taking a look at the MR for 10.1.x
Applied the fix locally
Verified tests fail without the fix and obviously pass with it.
Manually test on Drupal 10.1.x standard profile
Edited the content view content type filter to remember last selection
Went to the content page and filtered by article
Went to another admin page
Went back to content page verified article is selected
Repeated X3
Code looks good so +1 from me.
Comment #70
xjmThe IS is missing a proposed resolution, as @smustgrave pointed out. I added it for posterity.
Comment #71
bserem commented@xjm thanks for the clarifications. Completely understandable
Comment #75
xjmOK, this looks now. Thanks everyone! Adding credit for helpful reviews, research, and improvements to the patch and merge request.
Committed to 10.1.x, 10.0.x, and 9.5.x. Thanks!
Comment #80
luenemannAh - this was backported to 9.5.
Comment #81
fluidstack commentedHi All,
For your information - There seems to be an issue when working with the "Remember the last selection" feature and Better Exposed Filters - https://www.drupal.org/project/better_exposed_filters/issues/3359182.
drupal 9.4.15 and 9.5.9
I have been scratching my head for a while on this issue.
If any one has any thoughts, welcome.
Comment #82
ivnishDrupal 10.3: if your filters are not saved, needs to disable cache for this view
Comment #83
xjmAmending attribution.