You are here

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

Same name and namespace in other branches
  1. 8.5 src/Element/WebformTermsOfService.php \Drupal\webform\Element\WebformTermsOfService::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

src/Element/WebformTermsOfService.php, line 63

Class

WebformTermsOfService
Provides a webform terms of service element.

Namespace

Drupal\webform\Element

Code

public static function preRenderCheckbox($element) {
  $element = parent::preRenderCheckbox($element);
  $id = 'webform-terms-of-service-' . implode('_', $element['#parents']);
  if (empty($element['#title'])) {
    $element['#title'] = (string) t('I agree to the {terms of service}.');
  }
  $element['#title'] = str_replace('{', '<a role="button" href="#terms">', $element['#title']);
  $element['#title'] = str_replace('}', '</a>', $element['#title']);

  // Change description to render array.
  if (isset($element['#description'])) {
    $element['#description'] = [
      'description' => is_array($element['#description']) ? $element['#description'] : [
        '#markup' => $element['#description'],
      ],
    ];
  }
  else {
    $element['#description'] = [];
  }

  // Add terms to #description.
  $element['#description']['terms'] = [
    '#type' => 'container',
    '#attributes' => [
      'id' => $id . '--description',
      'class' => [
        'webform-terms-of-service-details',
        'js-hide',
      ],
    ],
  ];
  if (!empty($element['#terms_title'])) {
    $element['#description']['terms']['title'] = [
      '#type' => 'container',
      '#markup' => $element['#terms_title'],
      '#attributes' => [
        'class' => [
          'webform-terms-of-service-details--title',
        ],
      ],
    ];
  }
  if (!empty($element['#terms_content'])) {
    $element['#description']['terms']['content'] = is_array($element['#terms_content']) ? $element['#terms_content'] : [
      '#markup' => $element['#terms_content'],
    ];
    $element['#description']['terms']['content'] += [
      '#type' => 'container',
      '#attributes' => [
        'class' => [
          'webform-terms-of-service-details--content',
        ],
      ],
    ];
  }

  // Add accessibility attributes to title and content.
  if ($element['#type'] === static::TERMS_SLIDEOUT) {
  }

  // Set type to data attribute.
  // @see Drupal.behaviors.webformTermsOfService.
  $element['#wrapper_attributes']['data-webform-terms-of-service-type'] = $element['#terms_type'];
  $element['#attached']['library'][] = 'webform/webform.element.terms_of_service';

  // Change #type to checkbox so that element is rendered correctly.
  $element['#type'] = 'checkbox';
  $element['#wrapper_attributes']['class'][] = 'form-type-webform-terms-of-service';
  $element['#wrapper_attributes']['class'][] = 'js-form-type-webform-terms-of-service';
  $element['#element_validate'][] = [
    get_called_class(),
    'validateWebformTermsOfService',
  ];
  return $element;
}