Recently I was working on a project which required webform and some webform workflow. To my satisfaction I used the Webform Workflows Element module which provide a simple workflow element using the core Workflow module. This module provides simple element to expose a workflow into the webform submission. Basically, you can set the state of the submission and, if you want, provide log messages. One for admins, one for submitter. Exactly what I needed. The problem I ran into was when I wanted to include the submitter log message into the e-mail after workflow state change. There was no such token! Furtunately, it's Drupal.
There are two basic hooks we need to implement in our custom module. The first one is mymodule_token_info in which we define what tokens are going to add and in which token group. So for this particular case the hook function would look like this:
/**
* Implements hook_token_info().
*/
function mymodule_token_info() {
$info = [];
$info['tokens']['webform_workflow']['transition-log-admin'] = [
'name' => t('Log message for admins.'),
'description' => t('The log message for admins.'),
'dynamic' => TRUE,
];
$info['tokens']['webform_workflow']['transition-log-submitter'] = [
'name' => t('Log message for submitter.'),
'description' => t('The log message for submitter.'),
'dynamic' => TRUE,
];
return $info;
}Now the application knows about our new hooks we are going to implement. But it does not know what values to assign into them. For that there is a hook_tokens:
/**
* Implements hook_tokens().
*/
function mymodule_tokens($type, $tokens, array $data, array $options, \Drupal\Core\Render\BubbleableMetadata $bubbleable_metadata) {
$replacements = [];
if ($type === 'webform_workflow' && !empty($data['webform_submission'])) {
/** @var \Drupal\webform\WebformSubmissionInterface $webform_submission */
$webform_submission = $data['webform_submission'];
$bubbleable_metadata->addCacheableDependency($webform_submission);
$originalData = $webform_submission->getOriginalData();
$newData = $webform_submission->getData();
foreach ($tokens as $name => $original) {
switch ($name) {
case 'transition-log-admin':
$replacements[$original] = $newData['workflow']['log_admin'];
break;
case 'transition-log-submitter':
$replacements[$original] = $newData['workflow']['log_public'];
}
}
}
return $replacements;
}This way you'll get the appropriate data in the token and you can use it with automated e-mails after state change within your workflows.
Comments