View source
<?php
namespace Drupal\webform\Element;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Render\Element\Checkbox;
class WebformTermsOfService extends Checkbox {
const TERMS_SLIDEOUT = 'slideout';
const TERMS_MODAL = 'modal';
public function getInfo() {
return [
'#return_value' => TRUE,
'#terms_type' => static::TERMS_MODAL,
'#terms_title' => '',
'#terms_content' => '',
] + parent::getInfo();
}
public static function processCheckbox(&$element, FormStateInterface $form_state, &$complete_form) {
$element = parent::processCheckbox($element, $form_state, $complete_form);
if (empty($element['#required_error'])) {
$title = empty($element['#title']) ? (string) t('I agree to the {terms of service}.') : $element['#title'];
$title = str_replace('{', '', $title);
$title = str_replace('}', '', $title);
$element['#required_error'] = t('@name field is required.', [
'@name' => $title,
]);
}
return $element;
}
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']);
if (isset($element['#description'])) {
$element['#description'] = [
'description' => is_array($element['#description']) ? $element['#description'] : [
'#markup' => $element['#description'],
],
];
}
else {
$element['#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',
],
],
];
}
if ($element['#type'] === static::TERMS_SLIDEOUT) {
}
$element['#wrapper_attributes']['data-webform-terms-of-service-type'] = $element['#terms_type'];
$element['#attached']['library'][] = 'webform/webform.element.terms_of_service';
$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;
}
public static function validateWebformTermsOfService(&$element, FormStateInterface $form_state, &$complete_form) {
$value = (bool) $form_state
->getValue($element['#parents'], []);
$element['#value'] = $value;
$form_state
->setValueForElement($element, $value);
}
}