Create a webform submit Handler

Last updated on 23/10/2017

A Webform submit handler must be integrated as Plugin extending WebformHandlerBase.

To create for example a handler which is going to redirect to a specific page based on submitted data you'll have to create a plugin located on my_module/src/Plugin/WebformHandler/MyModuleRedirect.php

namespace Drupal\my_module\Plugin\WebformHandler;
 
use Drupal\Core\Form\FormStateInterface;
use Drupal\webform\Plugin\WebformHandlerBase;
use Drupal\webform\WebformSubmissionInterface;
 
/**
 * Form submission handler.
 *
 * Redirects to the Patient view Entity after the submit.
 *
 * @WebformHandler(
 *   id = "my_module_redirect",
 *   label = @Translation("Redirect to the ..."),
 *   category = @Translation("Webform Handler"),
 *   description = @Translation("Redirect to the ..."),
 *   cardinality =
 *       \Drupal\webform\Plugin\WebformHandlerInterface::CARDINALITY_SINGLE,
 *   results =
 *    \Drupal\webform\Plugin\WebformHandlerInterface::RESULTS_PROCESSED,
 * )
 */
class MyModuleRedirect extends WebformHandlerBase {
 
  public function confirmForm(array &$form, FormStateInterface $form_state, WebformSubmissionInterface $webform_submission) {
 
    $values = $webform_submission->getData();
 
    if (!empty($values['some_data'])) {
      $form_state->setRedirect('some_route.view', [
        'some_id' => $values['some_data'],
      ]);
    }
 
  }
}