If you try to use #ajax on a Views exposed filter form element, the callback is never invoked. This was discussed at length in the Views issue queue here:

#1183418: Ajax attached to exposed filters in form_alter doesn't trigger callback

The same bug exists in the version of Views in Drupal 8 core. As merlinofchaos wrote:

Views are $_GET forms and they specifically remove the form_id and form_build_id and form_token because they are 'submitted' even when they aren't submitted. That means the standard #ajax tools don't work with them, since those rely on being able to cache the form in core's form cache. That system is to naive to work with core's filters. I've never really tried to do anything like this in D7 yet, so I don't know what the solutions might be, but I can say that it's going to provide very ugly URLs if you hack the form id back in.

The proposed solution is to add a little custom JS magic so that we identify all the form info via after_build callbacks, stash those in JS settings, and then inside the JS, we reattach the relevant form info to the #ajax-enabled form elements so that their callbacks can be properly invoked.

Comments

dww created an issue. See original summary.

dww’s picture

Title: Ajax attached to Views exposed filter form doesn't trigger callbacks » Ajax attached to Views exposed filter form does not trigger callbacks
Status: Active » Needs work
FileSize
5.01 KB

Partially working patch based on porting the latest code from barraponto at #1183418-106: Ajax attached to exposed filters in form_alter doesn't trigger callback to D8.

Lendude’s picture

Status: Needs work » Needs review
FileSize
34.38 KB
3.85 KB

Here is a failing test for this. Haven't actually tested it with the fix yet :-)

The lack of a form-id makes the ajax framework think we have a too large file upload and throws an error that has nothing to do with this, see screenshot taken during the test after the waitforajax()

Lendude’s picture

And now without the half finished line .....

The last submitted patch, 3: 2842525-3-TEST_ONLY.patch, failed testing.

Status: Needs review » Needs work

The last submitted patch, 4: 2842525-4-TEST_ONLY.patch, failed testing.

Lendude’s picture

Status: Needs work » Needs review
Issue tags: +JavaScript, +JavaScriptTest
FileSize
9.33 KB

Combined patch with a bit of a cleanup of #2, still fails, but with a different error now.

Status: Needs review » Needs work

The last submitted patch, 7: 2842525-7.patch, failed testing.

michielnugter’s picture

FileSize
2 KB
9.4 KB

Current fail is because the right triggering element cannot be found. Somehow on

core/lib/Drupal/Core/Form/FormBuilder.php line 1316 the $input['_triggering_element_name'] is empty while it definitly is in the _POST.

Patch is a small improvement in javascript and an incorrect by-reference.

Lendude’s picture

Status: Needs work » Needs review
FileSize
1.1 KB
9.62 KB

More cleanup.

while it definitely is in the _POST.

And this is were it goes wrong, because this is a GET form. When looking at \Drupal\Core\Form\FormBuilder::processForm you see that it just gets the GET data. So all the values that have been set in the form aren't there either, because they are done by POST. So even if the _triggering_element gets set, you still won't have any values to work with.

Status: Needs review » Needs work

The last submitted patch, 10: 2842525-10.patch, failed testing.

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.

joelpittet’s picture

@dww regarding the triggering element recently in D7 (may have been entityreference changes) was fixed by this which may be related #2546700: Wrong triggering element is detected when the clicked button has a unique #name but a label that was changed client-side

originalcode’s picture

Same issue using Drupal 8.3.2. I applied the patch but it returns me this error in the AJAX response: https://pastebin.com/gDhW6Jnd

Any suggestion?

danielbeeke’s picture

Status: Needs work » Needs review
FileSize
7.82 KB

peter-van-lierop and I have written a patch which we think fixes this issue.
We are not to sure about the underlaying code. So this needs a good review.

Status: Needs review » Needs work

The last submitted patch, 17: 2842525-17.patch, failed testing. View results
- codesniffer_fixes.patch Interdiff of automated coding standards fixes only.