View source
<?php
namespace Drupal\webform_test_handler\Plugin\WebformHandler;
use Drupal\Core\Access\AccessResult;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\webform\Plugin\WebformHandlerBase;
use Drupal\webform\WebformInterface;
use Drupal\webform\WebformSubmissionInterface;
class TestWebformHandler extends WebformHandlerBase {
public function defaultConfiguration() {
return [
'message' => 'One two one two this is just a test',
];
}
public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
$form['message'] = [
'#type' => 'textfield',
'#title' => $this
->t('Message'),
'#default_value' => $this->configuration['message'],
'#required' => TRUE,
];
return $form;
}
public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
parent::submitConfigurationForm($form, $form_state);
$this->configuration['message'] = $form_state
->getValue('message');
}
public function alterElements(array &$elements, WebformInterface $webform) {
$this
->displayMessage(__FUNCTION__);
}
public function alterElement(array &$element, FormStateInterface $form_state, array $context) {
$this
->displayMessage(__FUNCTION__);
}
public function overrideSettings(array &$settings, WebformSubmissionInterface $webform_submission) {
$this
->displayMessage(__FUNCTION__);
}
public function alterForm(array &$form, FormStateInterface $form_state, WebformSubmissionInterface $webform_submission) {
$this
->displayMessage(__FUNCTION__);
}
public function validateForm(array &$form, FormStateInterface $form_state, WebformSubmissionInterface $webform_submission) {
$this
->displayMessage(__FUNCTION__);
$value = $form_state
->getValue('element');
if ($value && !in_array($value, [
'access_allowed',
'submission_access_denied',
'element_access_denied',
])) {
$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
->displayMessage(__FUNCTION__);
}
public function confirmForm(array &$form, FormStateInterface $form_state, WebformSubmissionInterface $webform_submission) {
$this
->messenger()
->addStatus($this->configuration['message'], TRUE);
\Drupal::logger('webform.test_form')
->notice($this->configuration['message']);
$this
->displayMessage(__FUNCTION__);
}
public function preCreate(array &$values) {
$this
->displayMessage(__FUNCTION__);
}
public function postCreate(WebformSubmissionInterface $webform_submission) {
$this
->displayMessage(__FUNCTION__);
}
public function postLoad(WebformSubmissionInterface $webform_submission) {
$this
->displayMessage(__FUNCTION__);
}
public function preDelete(WebformSubmissionInterface $webform_submission) {
$this
->displayMessage(__FUNCTION__);
}
public function postDelete(WebformSubmissionInterface $webform_submission) {
$this
->displayMessage(__FUNCTION__);
}
public function preSave(WebformSubmissionInterface $webform_submission) {
$this
->displayMessage(__FUNCTION__);
}
public function postSave(WebformSubmissionInterface $webform_submission, $update = TRUE) {
$this
->displayMessage(__FUNCTION__, $update ? 'update' : 'insert');
}
public function access(WebformSubmissionInterface $webform_submission, $operation, AccountInterface $account = NULL) {
$this
->displayMessage(__FUNCTION__ . 'Submission');
$value = $webform_submission
->getElementData('element');
if ($value === 'submission_access_denied') {
$access_result = AccessResult::forbidden();
}
else {
$access_result = parent::access($webform_submission, $operation, $account);
}
return $access_result
->setCacheMaxAge(0);
}
public function preprocessConfirmation(array &$variables) {
$this
->displayMessage(__FUNCTION__);
$variables['message'] = '::preprocessConfirmation';
}
public function createHandler() {
$this
->displayMessage(__FUNCTION__);
}
public function updateHandler() {
$this
->displayMessage(__FUNCTION__);
}
public function deleteHandler() {
$this
->displayMessage(__FUNCTION__);
}
public function accessElement(array &$element, $operation, AccountInterface $account = NULL) {
$this
->displayMessage(__FUNCTION__);
$webform_submission = $this
->getWebformSubmission();
if ($webform_submission && $webform_submission
->getElementData('element') === 'element_access_denied') {
$access_result = AccessResult::forbidden();
}
else {
$access_result = parent::accessElement($element, $operation, $account);
}
return $access_result
->setCacheMaxAge(0);
}
public function createElement($key, array $element) {
$this
->displayMessage(__FUNCTION__);
}
public function updateElement($key, array $element, array $original_element) {
$this
->displayMessage(__FUNCTION__);
}
public function deleteElement($key, array $element) {
$this
->displayMessage(__FUNCTION__);
}
protected function displayMessage($method_name, $context1 = NULL) {
if (PHP_SAPI !== 'cli') {
$t_args = [
'@id' => $this
->getHandlerId(),
'@class_name' => get_class($this),
'@method_name' => $method_name,
'@context1' => $context1,
];
$this
->messenger()
->addStatus($this
->t('Invoked @id: @class_name:@method_name @context1', $t_args), TRUE);
\Drupal::logger('webform.test_form')
->notice('Invoked: @class_name:@method_name @context1', $t_args);
}
}
}