You are here

public static function WebformToggle::preRenderCheckbox in Webform 6.x

Same name and namespace in other branches
  1. 8.5 modules/webform_toggles/src/Element/WebformToggle.php \Drupal\webform_toggles\Element\WebformToggle::preRenderCheckbox()

Prepares a #type 'checkbox' render element for input.html.twig.

Parameters

array $element: An associative array containing the properties of the element. Properties used: #title, #value, #return_value, #description, #required, #attributes, #checked.

Return value

array The $element with prepared variables ready for input.html.twig.

Overrides Checkbox::preRenderCheckbox

File

modules/webform_toggles/src/Element/WebformToggle.php, line 39

Class

WebformToggle
Provides a webform element for entering a toggle.

Namespace

Drupal\webform_toggles\Element

Code

public static function preRenderCheckbox($element) {
  $element = parent::preRenderCheckbox($element);
  $element += [
    '#toggle_size' => 'medium',
    '#toggle_theme' => 'light',
    '#on_text' => '',
    '#off_text' => '',
  ];

  // Toggle heights.
  $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;
}