WebformToggle.php in Webform 6.x
File
modules/webform_toggles/src/Plugin/WebformElement/WebformToggle.php
View source
<?php
namespace Drupal\webform_toggles\Plugin\WebformElement;
use Drupal\webform\Plugin\WebformElement\Checkbox;
use Drupal\webform\WebformSubmissionInterface;
class WebformToggle extends Checkbox {
use WebformToggleTrait;
protected function defineDefaultProperties() {
$properties = [
'toggle_theme' => 'light',
'toggle_size' => 'medium',
'on_text' => '',
'off_text' => '',
'title_display' => 'after',
] + parent::defineDefaultProperties();
unset($properties['required']);
return $properties;
}
protected function formatTextItem(array $element, WebformSubmissionInterface $webform_submission, array $options = []) {
$value = $this
->getValue($element, $webform_submission, $options);
$format = $this
->getItemFormat($element);
switch ($format) {
case 'value':
$on_text = !empty($element['#on_text']) ? $element['#on_text'] : $this
->t('Yes');
$off_text = !empty($element['#off_text']) ? $element['#off_text'] : $this
->t('No');
return $value ? $on_text : $off_text;
case 'raw':
default:
return $value ? 1 : 0;
}
}
}