When Ajax is enabled to a Views block display with exposed form filter, submitting the filter causes the page the reload. This happens when using themes that use buttons as opposed to input types of submit or image (e.g. Bootstrap). The problem doesn't exist in any of the base themes, but since ajax_view.js only attaches the ajax functionality on those two input types, it fails on other cases (e.g. Bootstrap-related themes).
One part of the fix is to apply the correct dom_id class on views-view.html.twig, but the other part relies on actually recognizing button[type=submit] as one of the elements where the ajax exposed filter should work.
This issue is related to https://www.drupal.org/project/bootstrap/issues/2651008, but I can't find any core patches to ajax_view.js so far.
Comments
Comment #2
randell commentedComment #3
randell commentedComment #4
prashantgajare commented@randell - Can you specify which bootstrap theme you are using ? I am not able to replicate this issue.
Thanks!
Comment #5
randell commentedHey @prashantgajare, in the issue I linked, it already works on the Bootstrap theme, which uses Bootstrap 3. The fix was more like a work-around to get it to work on the theme. But when using the Bootstrap Barrio theme, with Bootstrap 4, the issue persists, obviously because the fix was only applied to the Bootstrap theme. I can only guess that the issue also exists in other custom themes using Bootstrap. I think it's quite impractical to apply the same fix across multiple themes, when it can be fixed in core.
If you look at the patch, you'll get what I mean.
Comment #6
borisson_I'm not sure about this, this could be a BC break? It's also sad that we have to make changes to our code because bootstrap changes out the element
Comment #7
randell commentedI think the net effect is we're making views exposed form more flexible when it comes to form submissions. It has become a wide practice to use button elements over input submit and images, because buttons can contain other HTML elements as the labels. Plus, buttons can accept a wide range of uncommon but useful attributes. See MDN web docs.
Comment #8
s3b0un3tHello,
I confirm the problem under the latest version of Drupal (v8.6.13).
The ajax submission of the exposed views form doesn't work if we replace the input[type="submit"] with button[type = "submit"].
The patch delivered by @randell resolves the problem.
Reproduced with the contributed theme Bootstrap Barrio.
Comment #9
s3b0un3tUpdate issue version to 8.6.13.
Comment #10
jmickelaI tested the patch, and while it does correctly submit the form via AJAX, it seems that Views is not honoring the form value. I'm setting up a Search API page, and whether I have AJAX enabled or not, if I turn the submit button into a button of type submit, the full text search exposed filter has no effect.
Breaking AJAX seems to be part of a larger problem.
Comment #11
osab commentedThank you @randell, this is a very useful patch #2 if we're using selector in the template.
Comment #12
lendudeI think this is BC safe, we are only adding elements to check for.
It does need an automated test though.
Comment #15
dom. commentedI found out the patch at #2 is good but does not work on latest drupal, therefore I move the issue to 9.1.x
I reupload the patch without an interdiff since it is exactly the same as #2 but applying to 9.1.x branch and also includes the changes in both the
ajax_view.es6.jsandajax_view.jsfiles.It still does not includes tests thought.
Comment #16
slayne40 commentedFor latest Drupal 8.9.x
Comment #18
tim-dielsHaving the same issue and not using the bootstrap theme. The latest patch works as expected. Thanks for this!
Comment #19
ilya.no commentedAttaching patches for D9 versions in case anyone needs them.
Comment #20
mglamanThis also breaks Media Library when using Layout Builder and it loads the frontend theme.
Comment #22
pbouchereau commentedThis is a duplicate of #1551534: Allow a button in an exposed forms to trigger ajax.
Comment #23
mglamanPer #22, linking.
Comment #24
effulgentsia commentedClosing as a duplicate of #1551534: Submit buttons themed as <button> element fail to trigger ajax in Views exposed forms per #22. I'll add issue credit to that issue for folks who worked on this one.
For the larger problem, see #1671190: Use <button /> form element type instead of <input type="submit" />.