RangeSliderWidget.php in Range Slider 8
File
src/Plugin/Field/FieldWidget/RangeSliderWidget.php
View source
<?php
namespace Drupal\range_slider\Plugin\Field\FieldWidget;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Field\WidgetBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\range_slider\RangeSliderTrait;
class RangeSliderWidget extends WidgetBase {
use RangeSliderTrait;
public const OPTION_NONE = '_none_';
public static function defaultSettings() {
return [
'orientation' => 'horizontal',
'output' => self::OPTION_NONE,
] + parent::defaultSettings();
}
public function settingsForm(array $form, FormStateInterface $form_state) {
$element['orientation'] = [
'#type' => 'select',
'#options' => $this
->getOrientationOptions(),
'#title' => $this
->t('Orientation'),
'#default_value' => $this
->getSetting('orientation'),
'#required' => TRUE,
];
$element['output'] = [
'#type' => 'select',
'#options' => [
self::OPTION_NONE => $this
->t('None'),
] + $this
->getOutputOptions(),
'#title' => $this
->t('Output'),
'#default_value' => $this
->getSetting('output'),
];
return $element;
}
public function settingsSummary() {
$summary = [];
$widget_settings = $this
->getSettings();
if (!empty($widget_settings['orientation'])) {
$summary[] = $this
->t('Orientation: @orientation', [
'@orientation' => ucfirst($widget_settings['orientation']),
]);
}
else {
$summary[] = $this
->t('No orientation');
}
if (!empty($widget_settings['output']) && $widget_settings['output'] !== self::OPTION_NONE) {
$summary[] = $this
->t('Output: @output', [
'@output' => ucfirst($widget_settings['output']),
]);
}
else {
$summary[] = $this
->t('No output');
}
return $summary;
}
public function formElement(FieldItemListInterface $items, $delta, array $element, array &$form, FormStateInterface $form_state) {
$field_settings = $this
->getFieldSettings();
$widget_settings = $this
->getSettings();
$element += [
'#type' => 'range_slider',
'#default_value' => $items[$delta]->value ?? NULL,
'#data-orientation' => $widget_settings['orientation'] ?? 'horizontal',
'#output' => $widget_settings['output'] === self::OPTION_NONE ? FALSE : $widget_settings['output'],
];
if (is_numeric($field_settings['min'])) {
$element['#min'] = $field_settings['min'];
}
if (is_numeric($field_settings['max'])) {
$element['#max'] = $field_settings['max'];
}
return $element;
}
}