Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
Problem/Motivation
If an exposed form is used on the same page with facets, it resets facet's URL parameters on each submit.
Proposed resolution
Add "Preserve query parameters from URL" option for the default exposed form plugin.
Remaining tasks
+ write patch
- write tests
- review
User interface changes
The "Preserve query parameters from URL" setting is added to the "Exposed form style" > "Settings" configuration form.
API changes
None.
Data model changes
The preserve_url_query_parameters
option is added to views_exposed_form
schema.
Comment | File | Size | Author |
---|---|---|---|
#45 | 2681953-interdiff-42-45.txt | 1.33 KB | Leksat |
#45 | 2681953-45.patch | 4.29 KB | Leksat |
Comments
Comment #2
Leksat CreditAttribution: Leksat at Amazee Labs commentedComment #3
Leksat CreditAttribution: Leksat at Amazee Labs commentedComment #4
Leksat CreditAttribution: Leksat at Amazee Labs commentedComment #5
dawehnerSome test coverage would be sort of nice.
You can use
$this->view->request
instead.Comment #6
dawehnerComment #8
Leksat CreditAttribution: Leksat at Amazee Labs commentedAfter some usage of #2, I found that it makes much more sense to preserve only some parameters, not all of them.
Comment #9
Leksat CreditAttribution: Leksat at Amazee Labs commentedComment #12
bceyssensReapplied patch #8 to the 3.x branch
Comment #13
yogeshmpawarComment #14
yogeshmpawarAny Update on this issue ?
Comment #15
rocketeerbkw CreditAttribution: rocketeerbkw at Amazee Labs commentedI was having issues using this with facets. The facet identifier (like
f[0]
) was being double encoded and not recognized. I fixed this by decoding the name of the hidden form element.Comment #18
seanB#15 worked for me! Thanks.
Comment #19
aspilicious CreditAttribution: aspilicious commentedThis patch needs tests, but it works great in production.
Comment #22
Fernly CreditAttribution: Fernly at District09 commentedComment #23
Jelle_SReroll of #15
Comment #25
MatthijsReroll of #23 with an additional
!empty()
check to prevent issues on views without this option set.Comment #28
PanchukPatch #25 works well for me.
Tested with 9.3.9 core version.
Comment #29
ranjith_kumar_k_u CreditAttribution: ranjith_kumar_k_u at Zyxware Technologies commentedRe-rolled #25 for 9.4
Comment #31
tim-dielsI was just investigating the exposed form and query parameters and found out this issue. Thanks for all the work done.
It is working for me in the following situation:
Comment #32
alexpottThis method should have an @inheritdoc.
This code doesn't look right. Even though there is some protection because a user has to enter the list a of query parameter names in the URL, I'm really not sure about allowing stuff from query parameters to become $form array names. If you manage to do # something then all sorts of interesting things become possible.
Comment #33
davemybes CreditAttribution: davemybes commentedWe've been using patch #23 for years and it's worked great for us. However, I'm building a new View with an exposed filter (no facets) that I want to use with Better Exposed Filters. With this patch in place, I cannot save the BEF settings to properly enable it. I get past the first page to choose it as the style for exposed forms, but cannot save the next settings page where you choose the widget for each exposed filter. The following error appears in the logs.
TypeError: explode(): Argument #2 ($string) must be of type string, array given in explode() (line 120 of /docroot/core/modules/views/src/Plugin/views/exposed_form/ExposedFormPluginBase.php)
If I remove the patch things work as normal, but of course, our page with facets on now breaks.
If I enable BEF with the patch removed, then add the patch back afterwards, the Views-generated page throws a fatal error and I once again cannot save the BEF settings:
Error: Cannot create references to/from string offsets in Drupal\Component\Utility\NestedArray::setValue() (line 155 of /docroot/core/lib/Drupal/Component/Utility/NestedArray.php)
This happens with both BEF 8.x-5.2 and 6.0.1, running on Drupal 9.3.22 with either patch #23 or patch #25.
Comment #34
nitin_lamaAdded method doc comment as per #32
Comment #35
nitin_lamaComment #36
itaran CreditAttribution: itaran commentedAdded a few lines to 2681953-29.patch to prevent empty params showing in the URL.
Comment #37
tim-diels@itaran, why did you work with an older patch? Why is there no interdiff for the reviewers to easily see what is changed? And you did not follow the suggestions alexpott made...
So we have no idea in what state your patch is in...
Comment #39
skyredwangTested #34 with Facets. Steps:
1. Go to edit the views of interest
2. Expand "Advanced" section
3. Click "Exposed form style: Settings"
4. Put
f
in the Preserve query parameters from URL5. Save and clear cache
It works.
Comment #40
sidgrafix CreditAttribution: sidgrafix commentedTested #36 Works Great (this feature should definitely be added)
+1 (awesomesauce) saved me from having to use hook_form_views_exposed_form_alter() to modify the submitted query parameters for the exposed filter.
Specifically useful when having other view blocks on page that use contextual filters with setting "provide default value" -> "Type: Query parameter" which updates the content in those blocks when the set parameters are passed by URL from custom links that provide those additional parameters.
Comment #41
NicklasMF CreditAttribution: NicklasMF as a volunteer commented#36 works for me as well.
Comment #42
tim-dielsI've added the code back from #34 and created an interdiff with #36.
Still need to address point 2 and 3 from #32
Comment #43
Lukas von BlarerComment #44
aaron.ferris CreditAttribution: aaron.ferris at Zoocha commentedPatch from #42 is working well for me, we have a search implementation that has custom query parameters that were being lost on exposed form search. Although that's not specifically facets, but I believe the issue is the same.
Comment #45
Leksat CreditAttribution: Leksat at Amazee Labs commentedConfirming the issue with Better Exposed Filters described in #33. Somehow, when BEF is used, the value already comes as an array. Attaching a patch with a fix.
Comment #47
maxilein CreditAttribution: maxilein commentedThe patch in #45 applies and works with D10.2.5.
With a Exposed form style:Basic (Exposed form in block:no)
Just for completeness if others are looking for orientation:
It does not work with: Basic (with layout) from the https://www.drupal.org/project/vefl module.
For BEF there is an extra patch based on this patch. https://www.drupal.org/project/better_exposed_filters/issues/3119947#com...