The RemotePostWebformHandler should provide an option for redirecting to a particular URL if the remote post fails.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

jimkeller created an issue. See original summary.

jimkeller’s picture

The attached patch uses a custom Event and EventSubscriber to perform a redirect if remote post fails in the RemotePostWebformHandler.

I really wanted to do this without having to explicitly call $response->send() in the event subscriber, but I could not find a way to get from the Plugin to either a controller, a form state, or an appropriate ResponseEvent that would let me simply return a $response or call setResponse().

I also tried using the EventSubscriber to catch a custom Exception, but found that to be tricky because the save() function of the base Entity class converts all uncaught Exceptions to EntityStorageException .

jrockowitz’s picture

Status: Active » Needs review

Status: Needs review » Needs work

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

jrockowitz’s picture

A lot of people are currently using and relying on the RemotePost handler. Any changes would require test coverage to prevent any unexpected regressions.

I feel this functionality could easily be added by extending the RemotePost handler via a contrib/custom module. I am not sure the core webform module needs to provide this functionality.

jrockowitz’s picture

@jimkeller Right now the most valuable contribution someone could make in regards to remote posting submission data is to document how to build a completely custom remote post handler since there is no way the webform module's core remote post handler can address every use case.

jimkeller’s picture

I contributed a similar patch for the D7 Webform Remote Post that took the approach of simply calling a hook when the POST failed ( https://www.drupal.org/project/webform_remote_post/issues/2787751 )

I took a different approach here because it was simple and because Webform seems to offer so many options out of the box (including redirecting to a thank you page). One more didn't seem like a big change but I agree that I might have a bit of an edge case.

Rather than build an entire custom post handler, which seems like overkill, could we trigger an event that could be subscribed to or call a hook on failure? Developers should be able to apply their own logic when a remote POST fails, which could include a redirect.

jrockowitz’s picture

Rather than build an entire custom post handler, which seems like overkill, could we trigger an event that could be subscribed to or call a hook on failure?

Adding events to the remote post and possibly all handlers is a great and forward thinking idea. I am 100% onboard with doing this but we need to do it with test coverage.

jrockowitz’s picture

Status: Needs work » Needs review
FileSize
3.4 KB

The attached patch provides some initial support to redirect to a URL if Remote Post fails

Status: Needs review » Needs work

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

jrockowitz’s picture

Status: Needs work » Needs review
FileSize
14.61 KB

@jimkeller I want to avoid adding any event subscribers until the pattern is well defined in Drupal core.
@see #3027625: Encourage the use of events over hooks

Your initial patch and comment about using Response->send() was very helpful. Thanks.

  • jrockowitz authored f7309ce on 8.x-5.x
    Issue #3033726 by jrockowitz, jimkeller: Provide option to redirect to a...

  • jrockowitz authored e1776b5 on 8.x-5.x
    Issue #3033726 by jrockowitz, jimkeller: Provide option to redirect to a...
jrockowitz’s picture

Status: Needs review » Fixed

I committed the patch. Please download the latest dev release to review.

For anyone needing additional custom error handling for remote posts, they should extend the RemotePostWebformHandler and override the handleError method.

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.