How To Create a Custom Webform Handler In Drupal

Alter the webform submission behaviour in a simple way

Sergio Guardiola Herrador
3 min readAug 29, 2024
Photo by Rubaitul Azad on Unsplash

When you use webforms in Drupal, the default behaviour doesn’t allow you to check whether an e-mail has been sent or not. On one side, the webform submission is created and stored; on the other, the e-mail is sent. They’re decoupled processes.

But, like it was my case, maybe you need to do both simultaneously. All you have to do is create your own handler and replace it with the existing one in your webform.

To do so, you have to create a custom module (or edit an existing one) and create a file CustomEmailHandler.php (you can put another name, but has to be the same as the class name) in custom_module/src/Plugin/WebformHandler (replacing custom_module with your module name).

As you can see in this custom handler, it creates two fields in the handler configuration to change the confirmation and exception messages.

<?php

namespace Drupal\custom_module\Plugin\WebformHandler;

use Drupal\webform\Plugin\WebformHandlerBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\webform\WebformSubmissionInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\Core\Render\Markup;
use…

--

--

Sergio Guardiola Herrador

I write articles in English and Spanish, mostly about programming, technology, travel, money, investing. You can find me here: https://sergioguardiola.net