Problem/Motivation

A page with multiple Views exposed forms with AJAX enabled will trigger all form instances on the page because the form ID is the same when one of the forms is added to the page via AJAX. Also, this Drupal 7 issue is the same issue, but I didn't have a need for the cache fix there: #1735096: Allow multiple instances of the same exposed filter form on a single page

Proposed resolution

Use the Views DOM ID to have unique form IDs.

Issue fork drupal-2968207

Command icon 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

Daniel Korte created an issue. See original summary.

daniel korte’s picture

Status: Active » Needs review
StatusFileSize
new4.4 KB

Status: Needs review » Needs work
dawehner’s picture

Category: Feature request » Task
+++ b/core/modules/views/src/Form/ViewsExposedForm.php
@@ -115,7 +115,7 @@ public function buildForm(array $form, FormStateInterface $form_state) {
-    $form['#id'] = Html::cleanCssIdentifier('views_exposed_form-' . $view->storage->id() . '-' . $display['id']);
+    $form['#id'] = Html::cleanCssIdentifier('views_exposed_form-' . $view->storage->id() . '-' . $display['id'] . '-' . $view->dom_id);

I'm curious whether we could add the dom ID as data attribute? This way existing HTML ID doesn't change, and as result the risk of breaking things is reduced. Does this make sense?

daniel korte’s picture

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

Good call. I ran into that exact issue. Here’s another try at it:

daniel korte’s picture

I guess this should probably not destroy other attributes...

dawehner’s picture

Issue tags: +Needs tests

@Daniel Korte
Thank you for implementing my suggestion.

In order to ensure we don't cause a regression again I am wondering whether we could add some tests for this issue.

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.

borisson_’s picture

Status: Needs review » Needs work

NW based on #9.

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.

yury n’s picture

Patch from #6 does not apply against 8.6.x anymore. Updated it to work with 8.6.x

nortmas’s picture

Great guys, thank you! Patch #12 works form me!

a.dmitriiev’s picture

Re-rolled the patch for 8.7.6, strange, but it seems to be the same as for 8.6.x, but somehow #12 doesn't apply to 8.7.5.

UPDATE: Sorry, my bad, the patch applied to 8.7.5.

a.dmitriiev’s picture

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.

sathish.redcrackle’s picture

StatusFileSize
new258.5 KB

The above patches are not working on Ajax enabled views. It reloads the page on button click.
Also attached the screenshot showing JS errors on view forms.

JS warning

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.

shiraz dindar’s picture

#14 is working for me. Also, I looked for AJAX errors but didn't see any.

pavnish’s picture

Assigned: Unassigned » pavnish

Need to check for 9.0 and 9.1

pavnish’s picture

Assigned: pavnish » Unassigned
mrinalini9’s picture

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

Rerolled patch for 9.1.x, please review.

weekbeforenext’s picture

The patch from #12 worked for 8.9.5. Thanks all!

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.

liam morland’s picture

There is another solution to this in #3163299: Ajax exposed filters not working for multiple instances of the same Views block placed on one page, including tests. I don't know which is the better solution.

liam morland’s picture

The above is the patch from #22 as a merge request against 9.2.x.

alejandro cabarcos’s picture

Patch #22 working! Thank you.

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.

leisurman’s picture

Using Drupal 9.2.9. After applying the patch #22. I still get the console error - Found 2 elements with non-unique id. I am printing out a solr search exposed view form. I am printing the view twice in the twig. 1 for desktop display and another that displays only for mobile display.

bobooon’s picture

StatusFileSize
new2.25 KB

Re-rolled for 9.3.x

ranjith_kumar_k_u’s picture

StatusFileSize
new2.25 KB
new687 bytes

Status: Needs review » Needs work

The last submitted patch, 33: 2968207-33.patch, failed testing. View results

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.

eduardo morales alberti’s picture

We tried patch #32 and seems to work disabling the caches, but when we enabled the drupal caches, the form has the ids duplicated.
So seems to be also related to cache.

abh.ai’s picture

StatusFileSize
new2.25 KB

Rerolled patch for 9.5.x

sahil.goyal’s picture

StatusFileSize
new1.43 KB
new1.2 KB

#38 does not get applied to 10.1x so i made it compatible so now it can applies cleany. creating interdiff giving some error so i couldn’t able to update it here!

szloredan’s picture

StatusFileSize
new2.37 KB

Reroll for 9.5.x

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

rcodina’s picture

Status: Needs work » Needs review

The MR seems outdated. Patches #39 and #40 looks fine.

smustgrave’s picture

Status: Needs review » Needs work
Issue tags: +Needs issue summary update

Previously tagged for tests that are still needed.

Also issue summary could use some love.

gobind singh’s picture

StatusFileSize
new1.43 KB

Re-rolled 10.1.x / 11.x

rcodina’s picture

Patch on #39 was already meant for 10.1.x and 11.x and it applies and passes the tests. However, as said in #43, we still need new tests.

cmd87 made their first commit to this issue’s fork.

d.steindl made their first commit to this issue’s fork.

d.steindl changed the visibility of the branch drupal-2968207-11.x to hidden.

d.steindl’s picture

Hi all!
I'm sorry, I've messed around with 11.x branch. I don't understand why, but I was not able to rebase it to 11.3.x.
After messing around I've reset the branch to the initial commit from october and force pushed the initial version back to 11.x.
.. sorry for the confusion.

After messing around with 11.x I've created a new branch (drupal-2968207-11.x) and MR for the 11.3.x version. I hope that's fine?

d.steindl changed the visibility of the branch v11.x to hidden.

Version: 11.x-dev » main

Drupal core is now using the main branch as the primary development branch. New developments and disruptive changes should now be targeted to the main branch.

Read more in the announcement.