View source
<?php
namespace Drupal\webform_example_handler\Plugin\WebformHandler;
use Drupal\Component\Utility\Xss;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Render\Markup;
use Drupal\webform\Plugin\WebformHandlerBase;
use Drupal\webform\WebformInterface;
use Drupal\webform\WebformSubmissionInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
class ExampleWebformHandler extends WebformHandlerBase {
protected $tokenManager;
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
$instance = parent::create($container, $configuration, $plugin_id, $plugin_definition);
$instance->tokenManager = $container
->get('webform.token_manager');
return $instance;
}
public function defaultConfiguration() {
return [
'message' => 'This is a custom message.',
'debug' => FALSE,
];
}
public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
$form['message'] = [
'#type' => 'fieldset',
'#title' => $this
->t('Message settings'),
];
$form['message']['message'] = [
'#type' => 'textfield',
'#title' => $this
->t('Message to be displayed when form is completed'),
'#default_value' => $this->configuration['message'],
'#required' => TRUE,
];
$form['development'] = [
'#type' => 'details',
'#title' => $this
->t('Development settings'),
];
$form['development']['debug'] = [
'#type' => 'checkbox',
'#title' => $this
->t('Enable debugging'),
'#description' => $this
->t('If checked, every handler method invoked will be displayed onscreen to all users.'),
'#return_value' => TRUE,
'#default_value' => $this->configuration['debug'],
];
return $this
->setSettingsParents($form);
}
public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
parent::submitConfigurationForm($form, $form_state);
$this->configuration['message'] = $form_state
->getValue('message');
$this->configuration['debug'] = (bool) $form_state
->getValue('debug');
}
public function alterElements(array &$elements, WebformInterface $webform) {
$this
->debug(__FUNCTION__);
}
public function overrideSettings(array &$settings, WebformSubmissionInterface $webform_submission) {
$this
->debug(__FUNCTION__);
}
public function alterForm(array &$form, FormStateInterface $form_state, WebformSubmissionInterface $webform_submission) {
$this
->debug(__FUNCTION__);
}
public function validateForm(array &$form, FormStateInterface $form_state, WebformSubmissionInterface $webform_submission) {
$this
->debug(__FUNCTION__);
if ($value = $form_state
->getValue('element')) {
$form_state
->setErrorByName('element', $this
->t('The element must be empty. You entered %value.', [
'%value' => $value,
]));
}
}
public function submitForm(array &$form, FormStateInterface $form_state, WebformSubmissionInterface $webform_submission) {
$this
->debug(__FUNCTION__);
}
public function confirmForm(array &$form, FormStateInterface $form_state, WebformSubmissionInterface $webform_submission) {
$message = $this->configuration['message'];
$message = $this
->replaceTokens($message, $this
->getWebformSubmission());
$this
->messenger()
->addStatus(Markup::create(Xss::filter($message)), FALSE);
$this
->debug(__FUNCTION__);
}
public function preCreate(array &$values) {
$this
->debug(__FUNCTION__);
}
public function postCreate(WebformSubmissionInterface $webform_submission) {
$this
->debug(__FUNCTION__);
}
public function postLoad(WebformSubmissionInterface $webform_submission) {
$this
->debug(__FUNCTION__);
}
public function preDelete(WebformSubmissionInterface $webform_submission) {
$this
->debug(__FUNCTION__);
}
public function postDelete(WebformSubmissionInterface $webform_submission) {
$this
->debug(__FUNCTION__);
}
public function preSave(WebformSubmissionInterface $webform_submission) {
$this
->debug(__FUNCTION__);
}
public function postSave(WebformSubmissionInterface $webform_submission, $update = TRUE) {
$this
->debug(__FUNCTION__, $update ? 'update' : 'insert');
}
public function preprocessConfirmation(array &$variables) {
$this
->debug(__FUNCTION__);
}
public function createHandler() {
$this
->debug(__FUNCTION__);
}
public function updateHandler() {
$this
->debug(__FUNCTION__);
}
public function deleteHandler() {
$this
->debug(__FUNCTION__);
}
public function createElement($key, array $element) {
$this
->debug(__FUNCTION__);
}
public function updateElement($key, array $element, array $original_element) {
$this
->debug(__FUNCTION__);
}
public function deleteElement($key, array $element) {
$this
->debug(__FUNCTION__);
}
protected function debug($method_name, $context1 = NULL) {
if (!empty($this->configuration['debug'])) {
$t_args = [
'@id' => $this
->getHandlerId(),
'@class_name' => get_class($this),
'@method_name' => $method_name,
'@context1' => $context1,
];
$this
->messenger()
->addWarning($this
->t('Invoked @id: @class_name:@method_name @context1', $t_args), TRUE);
}
}
}