Create a Webform Handler in Drupal 8

The newly built Webform module for Drupal 8 has a ton of improvements for users and developers; here’s how to use the Webform Handler to post form data┬áto a third party.

Creating a form handler

You’ll first need to create a module, or have a module already written that you’d like to integrate the handler into.

For simplicity’s sake, we’ll cover creating a new module for an example form handler.

You’ll need the following folder structure for your module:

- modules
- custom
- my_custom_form_handler
- src
- Plugin
- WebformHandler

You should then create your handler file in the WebformHandler directory. We’ll call this one ExampleFormHandler, and the file should follow this template:

<?php
namespace Drupal\my_custom_form_handler\Plugin\WebformHandler;

use Drupal\Core\Session\Account\Interface;
use Drupal\Core\Serialization\Yaml;
use Drupal\Core\Form\FormStateInterface;
use Drupal\webform\Plugin\WebformHandlerBase;
use Drupal\webform\WebformSubmissionInterface;

use Guzzle\Http\Client;
use Guzzle\Http\Exception\RequestException;

/**
 * Form submission handler.
 *
 * @WebformHandler(
 *   id = "example_form_handler",
 *   label = @Translation("Example form handler"),
 *   category = @Translation("Examples"),
 *   description = @Translation("An example form handler"),
 *   cardinality = Drupal\webform\Plugin\WebformHandlerInterface::CARDINALITY_SINGLE,
 *   results = Drupal\webform\Plugin\WebformHandlerInterface::RESULTS_PROCESSED,
 * )
 */
class ExampleFormHandler extends WebformHandlerBase {

  /**
   * {@inheritdoc}
   */
  public function submitForm(array &$form, FormStateInterface $form_state, WebformSubmissionInterface $webform_submission) {
    // Your code here.
  }
}

It really is as simple as that! The only step left is to write the code to send the submission off to a 3rd party.

public function submitForm(array &$form, FormStateInterface $form_state, WebformSubmissionInterface $webform_submission) {
  $client = new Drupal::httpClient();
  $response = $client->request('POST', 'https://<your_third_party_service_here>',
    'form_params' => [
      'first_name' => $webform_submission->getData('first_name')
    ]
  );

  $code = $response->getStatusCode();

  if ($code >= 400 || $code === 0) {
    // Handle the error
  }
}

Thanks to the following people who have provided edits to the post in the comments:
Joery Lemmens
John Lewis

Published by

Matt Arnold

Developer of bespoke addons for Sage 200c

6 thoughts on “Create a Webform Handler in Drupal 8”

    1. Hi Rajeev,

      Can you share your code on gist.github.com? That will allow me to debug your problem.

      Kind Regards,
      Matt

  1. use Drupal\webform\WebformHandlerBase;

    should now be:

    use Drupal\webform\Plugin\WebformHandlerBase;

    In newer version of Webform module.

  2. The Annotations need to be updated as well or you will still get an error:

    /**
    * Form submission handler.
    *
    * @WebformHandler(
    * id = “example_form_handler”,
    * label = @Translation(“Example form handler”),
    * category = @Translation(“Examples”),
    * description = @Translation(“An example form handler”),
    * cardinality = \Drupal\webform\WebformHandlerInterface::CARDINALITY_SINGLE,
    * results = \Drupal\webform\WebformHandlerInterface::RESULTS_PROCESSED,
    * )
    */

    should be:

    /**
    * Form submission handler.
    *
    * @WebformHandler(
    * id = “example_form_handler”,
    * label = @Translation(“Example form handler”),
    * category = @Translation(“Examples”),
    * description = @Translation(“An example form handler”),
    * cardinality = \Drupal\webform\Plugin\WebformHandlerInterface::CARDINALITY_SINGLE,
    * results = \Drupal\webform\Plugin\WebformHandlerInterface::RESULTS_PROCESSED,
    * )
    */

    1. Hi John,

      Thank you for commenting. I have changed the annotations to reflect your comment.

      Kind Regards,
      Matt

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.