I'm trying to redirect to a results page after a form that uses Ajax is submitted. The form is a semi long multi step form, so Ajax is enabled for the user experience.
I have created a new WebformHandler and I'm using the confirmForm()
method, but I'm having difficulties to make the redirect work. The 'Confirmation' setting in my Webform is set to 'None'. I also don't see errors in the logs.
namespace Drupal\burnout_module\Plugin\WebformHandler;
use Drupal\Core\Form\FormStateInterface;
use Drupal\webform\Plugin\WebformHandlerBase;
use Drupal\webform\WebformSubmissionInterface;
/**
* Send values to the matching API and get results back, redirects to results page
*
* @WebformHandler(
* id = "matching_api_handler",
* label = @Translation("Matching API"),
* category = @Translation("Webform Handler"),
* description = @Translation("Send values to the matching API and get results back, redirects to results page."),
* cardinality = \Drupal\webform\Plugin\WebformHandlerInterface::CARDINALITY_UNLIMITED,
* results = \Drupal\webform\Plugin\WebformHandlerInterface::RESULTS_PROCESSED,
* submission = \Drupal\webform\Plugin\WebformHandlerInterface::SUBMISSION_REQUIRED,
* )
*/
class MatchingApiWebformHandler extends WebformHandlerBase {
/**
* {@inheritdoc}
*/
public function confirmForm(array &$form, FormStateInterface $form_state, WebformSubmissionInterface $webform_submission) {
// Get an array of the values from the submission.
$values = $webform_submission->getData();
// $result = ... code for calculating results with form values
$url = \Drupal\Core\Url::fromRoute('burnout.match')->setRouteParameters(array('result' => json_encode($result)));
$form_state->setRedirectUrl($url);
}
}
I have also tried other possibilities:
- Using a RedirectResponse which gave me a 'Headers already sent' error
- AjaxResponse with a RedirectCommand, nothing happens, no errors
Hopefully someone can point me in the right direction.
Kind regards