Thumbnail

Create a Webform Handler that sends a notification to Slack

Thumbnail

Campbell Tilley

|

In addition to the comment notification hook I recently blogged about, I’ve created a custom Webform Handler that sends a notification to slack each time a new submission is made.

If you’re using Webforms on your site and you use Slack for your main communication channels, then this handler could come in handy for you or your clients.

Before we begin, make sure you have the Webform and Slack module installed on your site. For more information on how to configure the Slack module and begin the integration process, you will need to read my previous article on sending notifications from Drupal to Slack.

For this example I am going to be using a Contact and a Feedback form with the ID’s contact and feedback.

The handler code will need to live in the following directory and file to work:
/web/modules/custom/<custom-module>/src/Plugin/WebformHandler/<MyWebformHandler>.php

Now that we have our file, we need to declare our namespaces

namespace Drupal\<custom-module>\Plugin\WebformHandler;

use Drupal\Core\Form\FormStateInterface;
use Drupal\webform\Plugin\WebformHandlerBase;
use Drupal\webform\webformSubmissionInterface;
use Drupal\Core\Url;

Next we need to setup the DocBook comment to ensure our Handler is well presented.

/**
* Form submission handler.
*
* @WebformHandler(
*   id = "<custom-module>",
*   label = @Translation("<Title that will appear in the handler list>"),
*   category = @Translation("Form Handler"),
*   description = @Translation("Sends submission data to Slack."),
*   cardinality = \Drupal\webform\Plugin\WebformHandlerInterface::CARDINALITY_SINGLE,
*   results = \Drupal\webform\Plugin\WebformHandlerInterface::RESULTS_PROCESSED,
* )
*/

From here we can build out our class. Ensure that your class name is the same as your file name.

class <MyWebformHandler> extends WebformHandlerBase {

 /**
  * {@inheritdoc}
  */
 public function defaultConfiguration() {
   return [];
 }

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

   // Configure the slack settings and pull an array of the webform data
   $config = \Drupal::config('slack.settings');
   $channel = $config->get('slack_channel');
   $username = $config->get('slack_username');
   $values = $webform_submission->getData();
   $webform_submission->setSticky(!$webform_submission->isSticky())->save();
   $sid = $webform_submission->id();
   $webform = $this->getWebform();
   $wid = $webform->id();

   // The submission URL of the form
   $submission_url = Url::fromRoute('entity.webform_submission.canonical', ['webform' => $wid, 'webform_submission' => $sid], ['absolute' => TRUE])->toString();

   // Set the form name for the output message depending on which form has been submitted
   if ($wid == 'contact') {
     $type = 'Contact';
   }
   elseif ($wid == 'feedback'){
     $type = 'Feedback';
   }

   $output = strip_tags($values['name']) . " has made a new submission to the " . $type . " Webform. Click <$submission_url | here> to view.";

   // This will send your message to Slack.
   \Drupal::service('slack.slack_service')
     ->sendMessage( $output, $channel, $username);

   return true;
 }
}

This handler is currently only outputting the username, webform type and the submission URL. You can tailor the $output message with more or less data to suit your requirements.

We now have a working handler, but we still need to enable it on our webform.

Flush your cache drush cr then on your Drupal site navigate to Structure > Webforms > Build > Settings > Emails/Handlers > + Add handler. You should see your handler in the list with the title that you set in the label section of the DocBook comment. Add the handler, save, enjoy!

If you have any questions, feel free to ask below.

Add new comment

The content of this field is kept private and will not be shown publicly.

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.
  • Web page addresses and email addresses turn into links automatically.

Comments

  • Allowed HTML tags: <em> <strong> <cite> <blockquote cite> <ul type> <ol start type> <li> <dl> <dt> <dd> <p>
  • Lines and paragraphs break automatically.
  • Web page addresses and email addresses turn into links automatically.
  • Use [gist:#####] where ##### is your gist number to embed the gist
    You may also include a specific file within a multi-file gist with [gist:####:my_file].

Spread the word