Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
The RemotePostWebformHandler should provide an option for redirecting to a particular URL if the remote post fails.
Comment | File | Size | Author |
---|---|---|---|
#11 | 3033726-11.patch | 14.61 KB | jrockowitz |
| |||
#9 | 3033726-9.patch | 3.4 KB | jrockowitz |
#2 | webform_remote_post_error_redirect.patch | 6.65 KB | jimkeller |
Comments
Comment #2
jimkeller CreditAttribution: jimkeller commentedThe 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 .
Comment #3
jrockowitz CreditAttribution: jrockowitz as a volunteer and at The Big Blue House commentedComment #5
jrockowitz CreditAttribution: jrockowitz as a volunteer and at The Big Blue House commentedA 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.
Comment #6
jrockowitz CreditAttribution: jrockowitz as a volunteer and at The Big Blue House commented@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.
Comment #7
jimkeller CreditAttribution: jimkeller commentedI 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.
Comment #8
jrockowitz CreditAttribution: jrockowitz as a volunteer and at The Big Blue House commentedAdding 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.
Comment #9
jrockowitz CreditAttribution: jrockowitz as a volunteer and at The Big Blue House commentedThe attached patch provides some initial support to redirect to a URL if Remote Post fails
Comment #11
jrockowitz CreditAttribution: jrockowitz as a volunteer and at The Big Blue House commented@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.
Comment #14
jrockowitz CreditAttribution: jrockowitz as a volunteer and at The Big Blue House commentedI 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.