View source
<?php
namespace Drupal\webform_test_element\Plugin\WebformElement;
use Drupal\Core\Form\FormStateInterface;
use Drupal\webform\Plugin\WebformElementBase;
use Drupal\webform\WebformSubmissionInterface;
class WebformTestElement extends WebformElementBase {
public function prepare(array &$element, WebformSubmissionInterface $webform_submission = NULL) {
$this
->displayMessage(__FUNCTION__);
$element['#element_validate'][] = [
get_class($this),
'validate',
];
}
public function setDefaultValue(array &$element) {
$this
->displayMessage(__FUNCTION__);
}
public function formatHtml(array $element, WebformSubmissionInterface $webform_submission, array $options = []) {
$this
->displayMessage(__FUNCTION__);
return '<i>' . $this
->formatText($element, $webform_submission, $options) . '</i>';
}
public function formatText(array $element, WebformSubmissionInterface $webform_submission, array $options = []) {
$value = $this
->getValue($element, $webform_submission, $options);
$this
->displayMessage(__FUNCTION__);
return strtoupper($value);
}
public function preview() {
return [];
}
public function preCreate(array &$element, array &$values) {
$this
->displayMessage(__FUNCTION__);
}
public function postCreate(array &$element, WebformSubmissionInterface $webform_submission) {
$this
->displayMessage(__FUNCTION__);
}
public function postLoad(array &$element, WebformSubmissionInterface $webform_submission) {
$this
->displayMessage(__FUNCTION__);
}
public function preDelete(array &$element, WebformSubmissionInterface $webform_submission) {
$this
->displayMessage(__FUNCTION__);
}
public function postDelete(array &$element, WebformSubmissionInterface $webform_submission) {
$this
->displayMessage(__FUNCTION__);
}
public function preSave(array &$element, WebformSubmissionInterface $webform_submission) {
$this
->displayMessage(__FUNCTION__);
}
public function save(array &$element, WebformSubmissionInterface $webform_submission) {
$this
->displayMessage(__FUNCTION__);
}
public function postSave(array &$element, WebformSubmissionInterface $webform_submission, $update = TRUE) {
$this
->displayMessage(__FUNCTION__, $update ? 'update' : 'insert');
}
protected function displayMessage($method_name, $context1 = NULL) {
if (PHP_SAPI !== 'cli') {
$t_args = [
'@class_name' => get_class($this),
'@method_name' => $method_name,
'@context1' => $context1,
];
$this
->messenger()
->addStatus($this
->t('Invoked: @class_name:@method_name @context1', $t_args));
}
}
public static function validate(array &$element, FormStateInterface $form_state) {
\Drupal::messenger()
->addStatus(t('Invoked: Drupal\\webform_test_element\\Plugin\\WebformElement\\WebformTestElement::validate'));
}
}