View source
<?php
namespace Drupal\yamlform\Plugin\YamlFormElement;
use Drupal\Core\Form\FormStateInterface;
use Drupal\yamlform\Element\YamlFormRating as YamlFormRatingElement;
use Drupal\yamlform\YamlFormSubmissionInterface;
class YamlFormRating extends Range {
public function getDefaultProperties() {
$properties = parent::getDefaultProperties();
unset($properties['range__output'], $properties['range__output_prefix'], $properties['range__output_suffix']);
$properties += [
'default_value' => 0,
'star_size' => 'medium',
'reset' => FALSE,
];
return $properties;
}
public function prepare(array &$element, YamlFormSubmissionInterface $yamlform_submission) {
if (!isset($element['#step'])) {
$element['#step'] = 1;
}
parent::prepare($element, $yamlform_submission);
}
public function formatHtml(array &$element, $value, array $options = []) {
$format = $this
->getFormat($element);
switch ($format) {
case 'star':
if (!empty($options['email'])) {
return parent::formatText($element, $value, $options);
}
$build = [
'#value' => $value,
'#readonly' => TRUE,
] + $element;
return YamlFormRatingElement::buildRateIt($build);
default:
return parent::formatHtml($element, $value, $options);
}
}
public function getDefaultFormat() {
return 'star';
}
public function getFormats() {
return parent::getFormats() + [
'star' => $this
->t('Star'),
];
}
public function form(array $form, FormStateInterface $form_state) {
$form = parent::form($form, $form_state);
$form['number']['#title'] = $this
->t('Rating settings');
$form['number']['star_size'] = [
'#type' => 'select',
'#title' => $this
->t('Star size'),
'#options' => [
'small' => $this
->t('Small (@size)', [
'@size' => '16px',
]),
'medium' => $this
->t('Medium (@size)', [
'@size' => '24px',
]),
'large' => $this
->t('Large (@size)', [
'@size' => '32px',
]),
],
'#required' => TRUE,
];
$form['number']['reset'] = [
'#type' => 'checkbox',
'#title' => $this
->t('Show reset button'),
'#description' => $this
->t('If checked, a reset button will be placed before the rating element.'),
'#return_value' => TRUE,
];
return $form;
}
}