Drual 8 Webform as internal Notification System

Drupal 8 Webform Is Awesome ! (formally known as ymalform)  the idea of having a internal notification system is a cool idea and some times you need to implement it ...
lets see an example of how you would do it with the webform module

so first you need to figure out when you want to send an notification and then to who ... this will most likely come in the form of EventSubscribers see http://taggartjensen.com/code-snip/drupal-8-workbench-eventsubscriber-example

in the linked article i was showing an event subscriber for workbench moderation state change of a entity (node content_type) ... follow that example to get you rolling on Eventsubscribers

Once You worked out The triggers Example form submission or whatever now all you have to do is leverage webform inbuilt webform_submission entity.


How To Programmatically create a Webform submission



<?php

$storage = \Drupal::service('entity_type.manager');
$test = "Some Content";
$data = [
  'email'=> 'test@example.com',
  'message' => 'some message i want to send with vars ' .$test ,
  'name'  => 'admin guy',
  'subject'  => 'beer', 
];

$create_data = [
  'webform_id' => 'contact',
  'data' => $data,
];

$webform_submission = $storage->getStorage('webform_submission')->create($create_data);
$webform_submission->save();

// if you have emails set to send they will send automaticly due to event subscriber.
//dump($webform_submission);
?>


The above Shows you how to create a new webform submission entity on the fly .... but yes use dependency injection as '\Drupal::service' is not good practice.
the beutiy of this is that webform uses its own event subscriber to automajicly send email when you save a new webform submission 
(if you have them set up) see the basic webform "contact" form email handler settings.

continuing the awesome is now that the webrom submission entity has saved and an email has been sent ... all you need to do is create a view and block for the user to show them the submissions so they have a in app representation of the messages sent.

stick that block in there profile page and you sir/madam  can go home early.


Category: