View source
<?php
namespace Drupal\yamlform_test\Plugin\YamlFormHandler;
use Drupal\Core\Form\FormStateInterface;
use Drupal\yamlform\YamlFormHandlerBase;
use Drupal\yamlform\YamlFormInterface;
use Drupal\yamlform\YamlFormSubmissionInterface;
class TestYamlFormHandler extends YamlFormHandlerBase {
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, YamlFormInterface $yamlform) {
$this
->displayMessage(__FUNCTION__);
}
public function alterForm(array &$form, FormStateInterface $form_state, YamlFormSubmissionInterface $yamlform_submission) {
$this
->displayMessage(__FUNCTION__);
}
public function validateForm(array &$form, FormStateInterface $form_state, YamlFormSubmissionInterface $yamlform_submission) {
$this
->displayMessage(__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, YamlFormSubmissionInterface $yamlform_submission) {
$this
->displayMessage(__FUNCTION__);
}
public function confirmForm(array &$form, FormStateInterface $form_state, YamlFormSubmissionInterface $yamlform_submission) {
drupal_set_message($this->configuration['message'], 'status', TRUE);
\Drupal::logger('yamlform.test')
->notice($this->configuration['message']);
$this
->displayMessage(__FUNCTION__);
}
public function preCreate(array $values) {
$this
->displayMessage(__FUNCTION__);
}
public function postCreate(YamlFormSubmissionInterface $yamlform_submission) {
$this
->displayMessage(__FUNCTION__);
}
public function postLoad(YamlFormSubmissionInterface $yamlform_submission) {
$this
->displayMessage(__FUNCTION__);
}
public function preDelete(YamlFormSubmissionInterface $yamlform_submission) {
$this
->displayMessage(__FUNCTION__);
}
public function postDelete(YamlFormSubmissionInterface $yamlform_submission) {
$this
->displayMessage(__FUNCTION__);
}
public function preSave(YamlFormSubmissionInterface $yamlform_submission) {
$this
->displayMessage(__FUNCTION__);
}
public function postSave(YamlFormSubmissionInterface $yamlform_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,
];
drupal_set_message($this
->t('Invoked: @class_name:@method_name @context1', $t_args), 'status', TRUE);
\Drupal::logger('yamlform.test')
->notice('Invoked: @class_name:@method_name @context1', $t_args);
}
}
}