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.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

Leksat created an issue. See original summary.

Leksat’s picture

Issue summary: View changes
Status: Active » Needs review
FileSize
3.42 KB
Leksat’s picture

Title: Allow exposed for to preserve URL query parameters » Allow exposed form to preserve URL query parameters
Leksat’s picture

Issue summary: View changes
dawehner’s picture

Issue tags: +Needs tests

Some test coverage would be sort of nice.

+++ b/core/modules/views/src/Plugin/views/exposed_form/ExposedFormPluginBase.php
@@ -276,6 +285,28 @@ public function exposedFormAlter(&$form, FormStateInterface $form_state) {
+      foreach (\Drupal::request()->query->all() as $name => $value) {

You can use $this->view->request instead.

dawehner’s picture

Status: Needs review » Needs work

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.

Leksat’s picture

FileSize
4.03 KB

After some usage of #2, I found that it makes much more sense to preserve only some parameters, not all of them.

Leksat’s picture

Issue summary: View changes

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

Drupal 8.1.9 was released on September 7 and is the final bugfix release for the Drupal 8.1.x series. Drupal 8.1.x will not receive any further development aside from security fixes. Drupal 8.2.0-rc1 is now available and sites should prepare to upgrade to 8.2.0.

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

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

Drupal 8.2.6 was released on February 1, 2017 and is the final full bugfix release for the Drupal 8.2.x series. Drupal 8.2.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.3.0 on April 5, 2017. (Drupal 8.3.0-alpha1 is available for testing.)

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

bceyssens’s picture

Reapplied patch #8 to the 3.x branch

yogeshmpawar’s picture

Status: Needs work » Needs review
yogeshmpawar’s picture

Any Update on this issue ?

rocketeerbkw’s picture

I 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.

Status: Needs review » Needs work

The last submitted patch, 15: 2681953-15.patch, failed testing. View results

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.

seanB’s picture

#15 worked for me! Thanks.

aspilicious’s picture

Status: Needs work » Needs review

This patch needs tests, but it works great in production.

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.

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.

Fernly’s picture

Version: 8.6.x-dev » 8.8.x-dev
Jelle_S’s picture

Reroll of #15

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.

Matthijs’s picture

Reroll of #23 with an additional !empty() check to prevent issues on views without this option set.

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

Drupal 8 is end-of-life as of November 17, 2021. There will not be further changes made to Drupal 8. Bugfixes are now made to the 9.3.x and higher branches only. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.2.x-dev » 9.3.x-dev
Panchuk’s picture

Patch #25 works well for me.
Tested with 9.3.9 core version.

ranjith_kumar_k_u’s picture

Re-rolled #25 for 9.4

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

Drupal 9.3.15 was released on June 1st, 2022 and is the final full bugfix release for the Drupal 9.3.x series. Drupal 9.3.x will not receive any further development aside from security fixes. Drupal 9 bug reports should be targeted for the 9.4.x-dev branch from now on, and new development or disruptive changes should 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.

tim-diels’s picture

Status: Needs review » Reviewed & tested by the community

I 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:

alexpott’s picture

Status: Reviewed & tested by the community » Needs work
  1. +++ b/core/modules/views/src/Plugin/views/exposed_form/ExposedFormPluginBase.php
    @@ -103,6 +105,22 @@ public function buildOptionsForm(&$form, FormStateInterface $form_state) {
    +
    +  public function submitOptionsForm(&$form, FormStateInterface $form_state) {
    

    This method should have an @inheritdoc.

  2. +++ b/core/modules/views/src/Plugin/views/exposed_form/ExposedFormPluginBase.php
    @@ -273,6 +291,29 @@ public function exposedFormAlter(&$form, FormStateInterface $form_state) {
    +    if (!empty($this->options['preserve_url_query_parameters'])) {
    +      $query = \Drupal::request()->query->all();
    +      foreach ($this->options['preserve_url_query_parameters'] as $name) {
    +        if (!isset($form[$name]) && isset($query[$name]) && ($value = $query[$name]) !== '') {
    +          if (is_array($value)) {
    +            foreach (explode('&', UrlHelper::buildQuery($value, $name)) as $param) {
    +              list($name, $value) = explode('=', $param);
    +              $form[rawurldecode($name)] = [
    +                '#type' => 'hidden',
    +                '#value' => rawurldecode($value),
    +              ];
    +            }
    +          }
    +          else {
    +            $form[$name] = [
    +              '#type' => 'hidden',
    +              '#value' => $value,
    +            ];
    +          }
    +        }
    +      }
    +    }
    

    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.

  3. We still need automated test coverage of the new feature
davemybes’s picture

We'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.

nitin_lama’s picture

Assigned: Unassigned » nitin_lama
FileSize
4.09 KB
749 bytes

Added method doc comment as per #32

nitin_lama’s picture

Assigned: nitin_lama » Unassigned
itaran’s picture

Added a few lines to 2681953-29.patch to prevent empty params showing in the URL.

tim-diels’s picture

@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...

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

Drupal 9.4.9 was released on December 7, 2022 and is the final full bugfix release for the Drupal 9.4.x series. Drupal 9.4.x will not receive any further development aside from security fixes. Drupal 9 bug reports should be targeted for the 9.5.x-dev branch from now on, and new development or disruptive changes should 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.

skyredwang’s picture

Tested #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 URL
5. Save and clear cache

It works.

sidgrafix’s picture

Tested #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.

NicklasMF’s picture

#36 works for me as well.

tim-diels’s picture

Lukas von Blarer’s picture

aaron.ferris’s picture

Patch 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.

Leksat’s picture

Confirming 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.

Version: 9.5.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. For more information, see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

maxilein’s picture

The 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...