View source
<?php
namespace Drupal\webform\Plugin\WebformElement;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Render\Markup;
use Drupal\Core\Template\Attribute;
use Drupal\webform\WebformSubmissionInterface;
class Range extends NumericBase {
protected function defineDefaultProperties() {
$properties = [
'min' => 0,
'max' => 100,
'step' => 1,
'output' => '',
'output__field_prefix' => '',
'output__field_suffix' => '',
'output__attributes' => [],
] + parent::defineDefaultProperties();
unset($properties['size'], $properties['minlength'], $properties['maxlength'], $properties['placeholder'], $properties['autocomplete'], $properties['format_items'], $properties['format_items_html'], $properties['format_items_text']);
return $properties;
}
public function prepare(array &$element, WebformSubmissionInterface $webform_submission = NULL) {
parent::prepare($element, $webform_submission);
$element += [
'#min' => $this
->getDefaultProperty('min'),
'#max' => $this
->getDefaultProperty('max'),
];
if (empty($element['#output'])) {
return;
}
$webform_key = isset($element['#webform_key']) ? $element['#webform_key'] : 'range';
if (in_array($element['#output'], [
'above',
'below',
])) {
$element += [
'#output__attributes' => [],
];
$attributes = new Attribute($element['#output__attributes']);
$attributes['for'] = $webform_key;
$attributes['data-display'] = $element['#output'];
if (isset($element['#output__field_prefix'])) {
$attributes['data-field-prefix'] = $element['#output__field_prefix'];
}
if (isset($element['#output__field_suffix'])) {
$attributes['data-field-suffix'] = $element['#output__field_suffix'];
}
$element['#children'] = Markup::create('<output' . $attributes . '></output>');
}
else {
$output = [
'#type' => 'number',
'#title' => $element['#title'],
'#title_display' => 'invisible',
'#id' => $webform_key . '__output',
'#name' => $webform_key . '__output',
];
$properties = [
'#min',
'#max',
'#step',
'#disabled',
];
$output += array_intersect_key($element, array_combine($properties, $properties));
foreach ($element as $key => $value) {
if (strpos($key, '#output__') === 0) {
$output_key = str_replace('#output__', '#', $key);
$output[$output_key] = $value;
}
}
if (!empty($output['#disabled'])) {
$output['#attributes']['disabled'] = TRUE;
}
$output['#attributes']['name'] = '';
$output['#attributes'] += [
'style' => '',
];
$output['#attributes']['style'] .= ($output['#attributes']['style'] ? ';' : '') . 'width:' . (strlen($element['#max'] . '') + 1) . 'em';
if ($element['#output'] === 'left') {
if (isset($element['#field_prefix'])) {
$element['#field_prefix'] = [
'output' => $output,
'delimiter' => [
'#markup' => '<span class="webform-range-output-delimiter"></span>',
],
'content' => is_array($element['#field_prefix']) ? $element['#field_prefix'] : [
'#markup' => $element['#field_prefix'],
],
];
}
else {
$element['#field_suffix'] = [
'output' => $output,
'delimiter' => [
'#markup' => '<span class="webform-range-output-delimiter"></span>',
],
];
}
}
else {
if (isset($element['#field_suffix'])) {
$element['#field_suffix'] = [
'content' => is_array($element['#field_suffix']) ? $element['#field_suffix'] : [
'#markup' => $element['#field_suffix'],
],
'delimiter' => [
'#markup' => '<span class="webform-range-output-delimiter"></span>',
],
'output' => $output,
];
}
else {
$element['#field_suffix'] = [
'delimiter' => [
'#markup' => '<span class="webform-range-output-delimiter"></span>',
],
'output' => $output,
];
}
}
}
$element['#attached']['library'][] = 'webform/webform.element.range';
}
protected function prepareElementValidateCallbacks(array &$element, WebformSubmissionInterface $webform_submission = NULL) {
parent::prepareElementValidateCallbacks($element, $webform_submission);
$element['#element_validate'][] = [
get_class($this),
'validateRange',
];
}
public function preview() {
return parent::preview() + [
'#min' => 0,
'#max' => 100,
'#step' => 1,
'#output' => 'below',
'#output__field_prefix' => '$',
'#output__field_suffix' => '.00',
];
}
public function form(array $form, FormStateInterface $form_state) {
$form = parent::form($form, $form_state);
$form['number']['#title'] = $this
->t('Range settings');
$form['output'] = [
'#type' => 'fieldset',
'#title' => $this
->t('Range output settings'),
];
$form['output']['output'] = [
'#type' => 'select',
'#title' => $this
->t("Output the range's value"),
'#empty_option' => $this
->t('- None -'),
'#options' => [
'right' => $this
->t('Right'),
'left' => $this
->t('Left'),
'above' => $this
->t('Above (Floating)'),
'below' => $this
->t('Below (Floating)'),
],
];
$form['output']['output_container'] = $this
->getFormInlineContainer() + [
'#states' => [
'visible' => [
':input[name="properties[output]"]' => [
'!value' => '',
],
],
],
];
$form['output']['output_container']['output__field_prefix'] = [
'#type' => 'textfield',
'#title' => $this
->t('Output prefix'),
'#description' => $this
->t('Text or code that is placed directly in front of the output. This can be used to prefix an output with a constant string. Examples=> $, #, -.'),
'#size' => 10,
];
$form['output']['output_container']['output__field_suffix'] = [
'#type' => 'textfield',
'#title' => $this
->t('Output suffix'),
'#description' => $this
->t('Text or code that is placed directly after the output. This can be used to add a unit to an output. Examples=> lb, kg, %.'),
'#size' => 10,
];
$form['output']['output__attributes'] = [
'#type' => 'webform_element_attributes',
'#title' => $this
->t('Output'),
'#classes' => $this->configFactory
->get('webform.settings')
->get('element.classes'),
'#states' => [
'visible' => [
':input[name="properties[output]"]' => [
'!value' => '',
],
],
],
];
return $form;
}
public static function validateRange(array &$element, FormStateInterface $form_state, array &$completed_form) {
$value = $element['#value'];
$value = $value === 0 ? '0' : (string) $value;
$form_state
->setValueForElement($element, $value);
}
}