Follow-up from #2074037: Add drupalPostUrl() — drupalPost()/drupalPostAjax() are for forms only, D8 JS performs non-form HTTP requests.
Updated: Comment #0
Problem/Motivation
I have been trying to use the 'new' drupalPost() method for an AJAX request, to find out that I need to update the current content based on its results - drupalPostAjaxForm() will do it, but I do not want to use it.
Proposed resolution
Make the code in drupalPostAjaxForm() that processes the AJAX response into the current content, as a protected method of its own.
This way we'd have a core method to update the current content after a drupalPost() call with AJAX header.
Otherwise each extending class will have to find its own solution in that case.
Remaining tasks
User interface changes
None.
API changes
No changes, just one method added.
Related Issues
Comment | File | Size | Author |
---|---|---|---|
#14 | 2087637-drupal_process_ajax_response-14.patch | 10.16 KB | mondrake |
#11 | 2087637-drupal_process_ajax_response-11.patch | 10.15 KB | mondrake |
#11 | interdiff_9-11.txt | 1.99 KB | mondrake |
#9 | 2087637-drupal_process_ajax_response-9.patch | 10.17 KB | mondrake |
#9 | interdiff_3-9.txt | 1.39 KB | mondrake |
Comments
Comment #1
mondrakePatch
Comment #3
mondrake#1 wouldn't have worked anyway :)
Comment #4
effulgentsia CreditAttribution: effulgentsia commentedIn general, this seems like a good decoupling, but can you give more detail on your use case for wanting to process an AJAX response outside of drupalPostAjaxForm()? Is there somewhere in core that would need to do that, or is this strictly for contrib's benefit?
Comment #5
mondrakeI was trying to refactor the ViewUI Preview tests developed for testing pager navigation within the Views UI preview form in #2048309: Views UI Preview - navigation is broken, see comment #2.
There, when we click on link, ajax.js sends a POST request that is not (fully) processed via the form controller, as there is no form submission and hence no 'triggering element' from a form. ajax.js also sends an additional
'js' => 'true'
post key which I used in the ViewPreviewFormController to detect that the preview needs to be refreshed, in place of checking for the unavailable triggering element. (see discussion in #2074037: Add drupalPostUrl() — drupalPost()/drupalPostAjax() are for forms only, D8 JS performs non-form HTTP requests, comments #5 to #13).However, the response is used by ajax.js to refresh the page content.
For the test I used the (now) drupalPostAjaxForm method, however this brings up a lot of form related info and I could not mimic the actual ajax.js behavior. So I turned to the 'new' drupalPost, which works fine - but falls short of updating the current content.
So options
As an example, specifically for this View Preview test, this would be before and after this patch
Before
After
The 'after' though is really aligned to the actual 'post' keys that ajax.js uses for processing a link click (not a form button).
Comment #7
mondrake#3: 2087637-drupal_process_ajax_response-3.patch queued for re-testing.
Comment #9
mondrake$drupal_settings has to be pre-saved like $content
Comment #10
effulgentsia CreditAttribution: effulgentsia commentedThanks for the use case in #5; totally makes sense.
The JSON data *is* the array of commands, so remove the word "and".
Looks like we're still capitalizing AJAX elsewhere in this docblock, so let's do that here too, for consistency.
I don't think $drupal_settings should be optional. There's always a non-empty drupalSettings on the original page, so calling this without passing that will result in incorrect test emulation.
Comment #11
mondrakeThank you @effulgentsia. Here we go.
Shall I also add the refactoring of Drupal\views_ui\Tests\PreviewTest::clickPreviewLinkAJAX() as per #5 here, or leave that for a separate issue?
Comment #12
effulgentsia CreditAttribution: effulgentsia commentedGreat. Thanks. I think the Views test refactor can be a separate issue.
Comment #13
webchickPatch no longer applies.
Comment #14
mondrakeRe-roll. Was conflicting with #1614186: Coding standards for using "native" PHP classes like stdClass in namespaced code.
Comment #15
mondrakeBack to RTBC
Comment #16
alexpottCommitted cfc1584 and pushed to 8.x. Thanks!
Lets create a change notice to let people know.
Comment #17
mondrakeThere's no usage of this method yet, apart from the obvious split in drupalPostAjaxForm. #2100323: Improve views UI preview tests will introduce an example which I suggest to refer to in the change record.
Comment #18
mondrakeProposal for review
Added a drupalProcessAjaxResponse() method to WebTestBase
Summary:
Code example - from the views preview UI test:
Note: content and drupalSettings need to be stored away before invoking drupalPost(), and then passed back to drupalProcessAjaxResponse(). This because drupalPost() replaces current content completely with the content returned by the AJAX controller, which is a sequence of AJAX commands (not HTML).
Comment #19
xjmTagging "Missing change record", as completing the change record updates here blocks a beta: #2083415: [META] Write up all outstanding change notices before release
The patch for this issue was committed on September 28, 2013, meaning that the change record for this issue has been outstanding for more than four months. The proposed draft looks pretty good to me; I'd just make it clearer in the bullet points at the top that we're talking about simpletests. E.g.:
With that change, I'd say go ahead and post the proposed change record, and restore this issue's status/priority/title/tags/etc. :)
Comment #20
mondrakeAdded change record: https://drupal.org/node/2185955