TestWebformVariant.php in Webform 8.5
File
tests/modules/webform_test_variant/src/Plugin/WebformVariant/TestWebformVariant.php
View source
<?php
namespace Drupal\webform_test_variant\Plugin\WebformVariant;
use Drupal\Core\Form\FormStateInterface;
use Drupal\webform\Plugin\WebformVariantBase;
use Drupal\webform\WebformInterface;
class TestWebformVariant extends WebformVariantBase {
public function defaultConfiguration() {
return [
'debug' => FALSE,
];
}
public function isApplicable(WebformInterface $webform) {
return strpos($webform
->id(), 'test_variant_') === 0 || strpos($webform
->id(), 'example_variant_') === 0;
}
public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
$form['development'] = [
'#type' => 'details',
'#title' => $this
->t('Development settings'),
];
$form['development']['debug'] = [
'#type' => 'checkbox',
'#title' => $this
->t('Enable debugging'),
'#description' => $this
->t('If checked, variant information will be displayed onscreen to all users.'),
'#return_value' => TRUE,
'#default_value' => $this->configuration['debug'],
'#parents' => [
'settings',
'debug',
],
];
return $form;
}
public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
$this->configuration = $form_state
->getValues();
$this->configuration['debug'] = (bool) $this->configuration['debug'];
}
public function applyVariant() {
$webform = $this
->getWebform();
if (!$this
->isApplicable($webform)) {
return FALSE;
}
$this
->debug();
return TRUE;
}
protected function debug() {
if (empty($this->configuration['debug'])) {
return;
}
$this
->messenger()
->addWarning('The test variant has been applied');
}
}