View source
<?php
namespace Drupal\webform\Element;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Render\Element\Range;
use Drupal\Core\Render\Element;
use Drupal\webform\Utility\WebformElementHelper;
class WebformRating extends Range {
public function getInfo() {
$class = get_class($this);
return [
'#min' => 0,
'#max' => 5,
'#step' => 1,
'#star_size' => 'medium',
'#reset' => FALSE,
'#process' => [
[
$class,
'processWebformRating',
],
],
'#pre_render' => [
[
$class,
'preRenderWebformRating',
],
],
'#theme' => 'input__webform_rating',
] + parent::getInfo();
}
public static function processWebformRating(&$element, FormStateInterface $form_state, &$complete_form) {
$element['#element_validate'] = [
[
get_called_class(),
'validateWebformRating',
],
];
return $element;
}
public static function preRenderWebformRating(array $element) {
$element['#attributes']['type'] = 'range';
$element['#attributes']['class'][] = 'js-webform-visually-hidden';
Element::setAttributes($element, [
'id',
'name',
'value',
'step',
'min',
'max',
]);
static::setAttributes($element, [
'form-webform-rating',
]);
if (isset($element['#attributes']['value']) && $element['#attributes']['value'] === '') {
$element['#attributes']['value'] = $element['#attributes']['min'];
}
$element['#children']['rateit'] = static::buildRateIt($element);
return $element;
}
public static function buildRateIt(array $element) {
$element += [
'#min' => 0,
'#max' => 5,
'#step' => 1,
'#star_size' => 'medium',
'#reset' => FALSE,
];
$is_readonly = !empty($element['#readonly']) || !empty($element['#attributes']['readonly']);
$attributes = [
'class' => [
'rateit',
'svg',
],
'data-rateit-min' => $element['#min'],
'data-rateit-max' => $element['#max'],
'data-rateit-step' => $element['#step'],
'data-rateit-resetable' => !$is_readonly && $element['#reset'] ? 'true' : 'false',
'data-rateit-readonly' => $is_readonly ? 'true' : 'false',
];
if (isset($element['#attributes']['data-drupal-selector'])) {
$attributes['data-rateit-backingfld'] = '[data-drupal-selector="' . $element['#attributes']['data-drupal-selector'] . '"]';
}
if (isset($element['#value'])) {
$attributes['data-rateit-value'] = $element['#value'];
}
if (isset($element['#starwidth']) && isset($element['#starheight'])) {
$attributes['data-rateit-starwidth'] = $element['#starwidth'];
$attributes['data-rateit-starheight'] = $element['#starheight'];
}
else {
$sizes = [
'large' => 32,
'medium' => 24,
'small' => 16,
];
$size = isset($sizes[$element['#star_size']]) ? $element['#star_size'] : 'small';
$attributes['data-rateit-starwidth'] = $attributes['data-rateit-starheight'] = $sizes[$size];
$attributes['class'][] = 'rateit-' . $size;
}
return [
'#type' => 'html_tag',
'#tag' => 'div',
'#attributes' => $attributes,
'#attached' => [
'library' => [
'webform/webform.element.rating',
],
],
];
}
public static function validateWebformRating(&$element, FormStateInterface $form_state, &$complete_form) {
$value = $element['#value'];
if (Element::isVisibleElement($element) && !empty($element['#required']) && ($value === '0' || $value === '')) {
WebformElementHelper::setRequiredError($element, $form_state);
}
}
}