View source
<?php
namespace Drupal\webform_toggles\Element;
use Drupal\Core\Render\Element\Checkbox;
class WebformToggle extends Checkbox {
public function getInfo() {
return static::getDefaultProperties() + parent::getInfo();
}
public static function getDefaultProperties() {
return [
'#toggle_theme' => 'light',
'#toggle_size' => 'medium',
'#on_text' => '',
'#off_text' => '',
];
}
public static function preRenderCheckbox($element) {
$element = parent::preRenderCheckbox($element);
$element += [
'#toggle_size' => 'medium',
'#toggle_theme' => 'light',
'#on_text' => '',
'#off_text' => '',
];
$sizes = [
'large' => 36,
'medium' => 24,
'small' => 16,
];
$height = $sizes[$element['#toggle_size']];
if (!empty($element['#on_text']) || !empty($element['#off_text'])) {
$width = $height * 3;
}
else {
$width = $height * 2;
}
$attributes = [
'class' => [
'js-webform-toggle',
'webform-toggle',
'toggle',
'toggle-' . $element['#toggle_size'],
'toggle-' . $element['#toggle_theme'],
],
'data-toggle-height' => $height,
'data-toggle-width' => $width,
'data-toggle-text-on' => $element['#on_text'],
'data-toggle-text-off' => $element['#off_text'],
];
$element['#children']['toggles'] = [
'#type' => 'html_tag',
'#tag' => 'div',
'#attributes' => $attributes,
'#attached' => [
'library' => [
'webform_toggles/webform_toggles.element',
],
],
];
return $element;
}
}