View source
<?php
namespace Drupal\maestro_webform\Plugin\WebformHandler;
use Drupal\Core\Form\FormStateInterface;
use Drupal\webform\Plugin\WebformHandlerBase;
use Drupal\webform\WebformSubmissionInterface;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Logger\LoggerChannelFactoryInterface;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\webform\WebformSubmissionConditionsValidatorInterface;
use Drupal\webform\WebformTokenManagerInterface;
use Drupal\maestro\Engine\MaestroEngine;
use Symfony\Component\DependencyInjection\ContainerInterface;
class MaestroWebformHandler extends WebformHandlerBase {
protected $currentUser;
protected $moduleHandler;
protected $languageManager;
protected $configFactory;
protected $mailManager;
protected $tokenManager;
protected $themeManager;
protected $elementManager;
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
$instance = parent::create($container, $configuration, $plugin_id, $plugin_definition);
$instance->currentUser = $container
->get('current_user');
$instance->moduleHandler = $container
->get('module_handler');
$instance->languageManager = $container
->get('language_manager');
$instance->mailManager = $container
->get('plugin.manager.mail');
$instance->themeManager = $container
->get('webform.theme_manager');
$instance->tokenManager = $container
->get('webform.token_manager');
$instance->elementManager = $container
->get('plugin.manager.webform.element');
return $instance;
}
public function getSummary() {
$summary = parent::getSummary();
$template = MaestroEngine::getTemplate($this->configuration['maestro_template']);
$summary['#settings']['template_label'] = $template->label;
return $summary;
}
public function defaultConfiguration() {
return [
'maestro_template' => '',
'maestro_message_success' => '',
'maestro_message_failure' => '',
];
}
public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
$maestro_templates = MaestroEngine::getTemplates();
$templates = [];
$templates['none'] = $this
->t('Select Template');
foreach ($maestro_templates as $machine_name => $template) {
$templates[$machine_name] = $template->label;
}
$form['maestro_template'] = [
'#type' => 'select',
'#title' => $this
->t('Maestro Workflow Template'),
'#description' => $this
->t('The template you choose will be spawned when the webform submission occurs.'),
'#default_value' => $this->configuration['maestro_template'],
'#options' => $templates,
'#suffix' => $this
->t('Maestro will use a default unique_identifier called "submission" to track this content in the launched workflow.
Only <b>VALIDATED</b> templates will be spawned.'),
];
$form['maestro_message_success'] = [
'#type' => 'textfield',
'#title' => $this
->t('Message to user on submission'),
'#description' => $this
->t('When a user submits the webform, what message would you like to show the user (Uses a Drupal message). Leave blank for no message.'),
'#default_value' => $this->configuration['maestro_message_success'],
];
$form['maestro_message_failure'] = [
'#type' => 'textfield',
'#title' => $this
->t('Message to user when a Maestro process fails to start.'),
'#description' => $this
->t('If a Maestro Process fails to start, this message will be displayed to the end user (Uses a Drupal message). Leave blank for no message.'),
'#default_value' => $this->configuration['maestro_message_failure'],
];
return $form;
}
public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
parent::submitConfigurationForm($form, $form_state);
$values = $form_state
->getValues();
foreach ($this->configuration as $name => $value) {
if (isset($values[$name])) {
$this->configuration[$name] = $values[$name];
}
}
}
public function submitForm(array &$form, FormStateInterface $form_state, WebformSubmissionInterface $webform_submission) {
$webform_submission->data['maestro_skip'] = FALSE;
$maestroElements = $form_state
->getValue('maestro');
if ($maestroElements) {
$queueID = $maestroElements['queue_id'];
$templateTask = MaestroEngine::getTemplateTaskByQueueID($queueID);
if ($templateTask) {
$webform_submission->data['maestro_skip'] = $templateTask['data']['skip_webform_handlers'];
}
}
}
public function postSave(WebformSubmissionInterface $webform_submission, $update = TRUE) {
$maestro_skip = FALSE;
if (isset($webform_submission->data['maestro_skip'])) {
if ($webform_submission->data['maestro_skip']) {
$maestro_skip = TRUE;
}
}
else {
$maestro_skip = FALSE;
}
if (!$update && !$maestro_skip) {
$maestro = new MaestroEngine();
$processID = $maestro
->newProcess($this->configuration['maestro_template']);
if ($processID !== FALSE) {
if ($this->configuration['maestro_message_success'] != '') {
\Drupal::messenger()
->addStatus($this->configuration['maestro_message_success']);
}
if (!MaestroEngine::createEntityIdentifier($processID, $webform_submission
->getEntityTypeId(), $webform_submission
->bundle(), 'submission', $webform_submission
->id())) {
\Drupal::messenger()
->addError($this->configuration['maestro_message_failure']);
}
}
else {
if ($this->configuration['maestro_message_failure'] != '') {
\Drupal::messenger()
->addError($this->configuration['maestro_message_failure']);
}
}
}
}
public function isExcluded() {
return $this->configFactory
->get('webform.settings')
->get('handler.excluded_handlers.' . $this->pluginDefinition['id']) ? TRUE : FALSE;
}
public function isEnabled() {
return $this->status ? TRUE : FALSE;
}
}