class Sliders in Better Exposed Filters 8.4
Same name and namespace in other branches
- 8.5 src/Plugin/better_exposed_filters/filter/Sliders.php \Drupal\better_exposed_filters\Plugin\better_exposed_filters\filter\Sliders
JQuery UI slider widget implementation.
Plugin annotation
@BetterExposedFiltersFilterWidget(
id = "bef_sliders",
label = @Translation("jQuery UI Slider"),
)
Hierarchy
- class \Drupal\Component\Plugin\PluginBase implements DerivativeInspectionInterface, PluginInspectionInterface
- class \Drupal\better_exposed_filters\Plugin\BetterExposedFiltersWidgetBase implements BetterExposedFiltersWidgetInterface uses StringTranslationTrait
- class \Drupal\better_exposed_filters\Plugin\better_exposed_filters\filter\FilterWidgetBase implements BetterExposedFiltersWidgetInterface uses StringTranslationTrait
- class \Drupal\better_exposed_filters\Plugin\better_exposed_filters\filter\Sliders
- class \Drupal\better_exposed_filters\Plugin\better_exposed_filters\filter\FilterWidgetBase implements BetterExposedFiltersWidgetInterface uses StringTranslationTrait
- class \Drupal\better_exposed_filters\Plugin\BetterExposedFiltersWidgetBase implements BetterExposedFiltersWidgetInterface uses StringTranslationTrait
Expanded class hierarchy of Sliders
File
- src/
Plugin/ better_exposed_filters/ filter/ Sliders.php, line 16
Namespace
Drupal\better_exposed_filters\Plugin\better_exposed_filters\filterView source
class Sliders extends FilterWidgetBase {
// Slider animation options.
const ANIMATE_NONE = 'none';
const ANIMATE_SLOW = 'slow';
const ANIMATE_NORMAL = 'normal';
const ANIMATE_FAST = 'fast';
const ANIMATE_CUSTOM = 'custom';
// Slider orientation options.
const ORIENTATION_HORIZONTAL = 'horizontal';
const ORIENTATION_VERTICAL = 'vertical';
/**
* {@inheritdoc}
*/
public function defaultConfiguration() {
return parent::defaultConfiguration() + [
'min' => 0,
'max' => 99999,
'step' => 1,
'animate' => self::ANIMATE_NONE,
'animate_ms' => 0,
'orientation' => self::ORIENTATION_HORIZONTAL,
];
}
/**
* {@inheritdoc}
*/
public static function isApplicable($filter = NULL, array $filter_options = []) {
/** @var \Drupal\views\Plugin\views\filter\FilterPluginBase $filter */
$is_applicable = FALSE;
// The date filter handler extends the numeric filter handler so we have
// to exclude it specifically.
$is_numeric_filter = is_a($filter, 'Drupal\\views\\Plugin\\views\\filter\\NumericFilter');
$is_range_filter = is_a($filter, 'Drupal\\range\\Plugin\\views\\filter\\Range');
$is_date_filter = is_a($filter, 'Drupal\\views\\Plugin\\views\\filter\\Date');
if (($is_numeric_filter || $is_range_filter) && !$is_date_filter && !$filter
->isAGroup()) {
$is_applicable = TRUE;
}
return $is_applicable;
}
/**
* {@inheritdoc}
*/
public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
/** @var \Drupal\views\Plugin\views\filter\FilterPluginBase $filter */
$filter = $this->handler;
$form = parent::buildConfigurationForm($form, $form_state);
$form['min'] = [
'#type' => 'number',
'#title' => $this
->t('Range minimum'),
'#default_value' => $this->configuration['min'],
'#description' => $this
->t('The minimum allowed value for the jQuery range slider. It can be positive, negative, or zero and have up to 11 decimal places.'),
];
$form['max'] = [
'#type' => 'number',
'#title' => $this
->t('Range maximum'),
'#default_value' => $this->configuration['max'],
'#description' => $this
->t('The maximum allowed value for the jQuery range slider. It can be positive, negative, or zero and have up to 11 decimal places.'),
];
$form['step'] = [
'#type' => 'number',
'#title' => $this
->t('Step'),
'#default_value' => $this->configuration['step'],
'#description' => $this
->t('Determines the size or amount of each interval or step the slider takes between the min and max.') . '<br />' . $this
->t('The full specified value range of the slider (Range maximum - Range minimum) must be evenly divisible by the step.') . '<br />' . $this
->t('The step must be a positive number of up to 5 decimal places.'),
'#min' => 0,
];
$form['animate'] = [
'#type' => 'select',
'#title' => $this
->t('Animation speed'),
'#options' => [
self::ANIMATE_NONE => $this
->t('None'),
self::ANIMATE_SLOW => $this
->t('Slow'),
self::ANIMATE_NORMAL => $this
->t('Normal'),
self::ANIMATE_FAST => $this
->t('Fast'),
self::ANIMATE_CUSTOM => $this
->t('Custom'),
],
'#default_value' => $this->configuration['animate'],
'#description' => $this
->t('Whether to slide handle smoothly when user click outside handle on the bar.'),
];
$form['animate_ms'] = [
'#type' => 'number',
'#title' => $this
->t('Animation speed in milliseconds'),
'#default_value' => $this->configuration['animate_ms'],
'#description' => $this
->t('The number of milliseconds to run the animation (e.g. 1000).'),
'#states' => [
'visible' => [
':input[name="exposed_form_options[bef][filter][' . $filter->field . '][configuration][animate]"]' => [
'value' => self::ANIMATE_CUSTOM,
],
],
],
];
$form['orientation'] = [
'#type' => 'select',
'#title' => $this
->t('Orientation'),
'#options' => [
self::ORIENTATION_HORIZONTAL => $this
->t('Horizontal'),
self::ORIENTATION_VERTICAL => $this
->t('Vertical'),
],
'#default_value' => $this->configuration['orientation'],
'#description' => $this
->t('The orientation of the jQuery range slider.'),
];
return $form;
}
/**
* {@inheritdoc}
*/
public function validateConfigurationForm(array &$form, FormStateInterface $form_state) {
parent::validateConfigurationForm($form, $form_state);
// Max must be > min.
$min = $form_state
->getValue('min');
$max = $form_state
->getValue('max');
if (!empty($min) && $max <= $min) {
$form_state
->setError($form['max'], $this
->t('The slider max value must be greater than the slider min value.'));
}
// Step must have:
// - No more than 5 decimal places.
// - Slider range must be evenly divisible by step.
$step = $form_state
->getValue('step');
if (strlen(substr(strrchr((string) $step, '.'), 1)) > 5) {
$form_state
->setError($form['step'], $this
->t('The slider step option for %name cannot have more than 5 decimal places.'));
}
// Very small step and a vary large range can go beyond the max value of
// an int in PHP. Thus we look for a decimal point when casting the result
// to a string.
if (strpos((string) ($max - $min) / $step, '.')) {
$form_state
->setError($form['step'], $this
->t('The slider range must be evenly divisible by the step option.'));
}
}
/**
* {@inheritdoc}
*/
public function exposedFormAlter(array &$form, FormStateInterface $form_state) {
$field_id = $this
->getExposedFilterFieldId();
parent::exposedFormAlter($form, $form_state);
// Attach the JS (@see /js/sliders.js)
$form[$field_id]['#attached']['library'][] = 'better_exposed_filters/sliders';
// Set the slider settings.
$form[$field_id]['#attached']['drupalSettings']['better_exposed_filters']['slider'] = TRUE;
$form[$field_id]['#attached']['drupalSettings']['better_exposed_filters']['slider_options'][$field_id] = [
'min' => $this->configuration['min'],
'max' => $this->configuration['max'],
'step' => $this->configuration['step'],
'animate' => $this->configuration['animate'] === self::ANIMATE_CUSTOM ? $this->configuration['animate_ms'] : $this->configuration['animate'],
'orientation' => $this->configuration['orientation'],
'id' => Html::getUniqueId($field_id),
'dataSelector' => Html::getId($field_id),
'viewId' => $form['#id'],
];
}
}
Members
Name![]() |
Modifiers | Type | Description | Overrides |
---|---|---|---|---|
BetterExposedFiltersWidgetBase:: |
protected | property | The views plugin this configuration will affect when exposed. | |
BetterExposedFiltersWidgetBase:: |
protected | property | The views executable object. | |
BetterExposedFiltersWidgetBase:: |
protected | function | Sets metadata on the form elements for easier processing. | |
BetterExposedFiltersWidgetBase:: |
protected | function | Moves an exposed form element into a field group. | |
BetterExposedFiltersWidgetBase:: |
public | function |
Gets this plugin's configuration. Overrides ConfigurableInterface:: |
|
BetterExposedFiltersWidgetBase:: |
protected | function | Returns exposed form action URL object. | |
BetterExposedFiltersWidgetBase:: |
public | function |
Sets the configuration for this plugin instance. Overrides ConfigurableInterface:: |
|
BetterExposedFiltersWidgetBase:: |
public | function |
Sets the view object. Overrides BetterExposedFiltersWidgetInterface:: |
|
BetterExposedFiltersWidgetBase:: |
public | function |
Sets the exposed view handler plugin. Overrides BetterExposedFiltersWidgetInterface:: |
|
BetterExposedFiltersWidgetBase:: |
public | function |
Form submission handler. Overrides PluginFormInterface:: |
|
BetterExposedFiltersWidgetBase:: |
public | function |
Constructs a \Drupal\Component\Plugin\PluginBase object. Overrides PluginBase:: |
|
FilterWidgetBase:: |
protected | function | Helper function to get the unique identifier for the exposed filter. | |
FilterWidgetBase:: |
protected | function | Helper function to get the widget type of the exposed filter. | |
FilterWidgetBase:: |
public | function | Sorts the options for a given form element alphabetically. | |
PluginBase:: |
protected | property | Configuration information passed into the plugin. | 1 |
PluginBase:: |
protected | property | The plugin implementation definition. | 1 |
PluginBase:: |
protected | property | The plugin_id. | |
PluginBase:: |
constant | A string which is used to separate base plugin IDs from the derivative ID. | ||
PluginBase:: |
public | function |
Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface:: |
|
PluginBase:: |
public | function |
Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface:: |
|
PluginBase:: |
public | function |
Gets the definition of the plugin implementation. Overrides PluginInspectionInterface:: |
3 |
PluginBase:: |
public | function |
Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface:: |
|
PluginBase:: |
public | function | Determines if the plugin is configurable. | |
Sliders:: |
constant | |||
Sliders:: |
constant | |||
Sliders:: |
constant | |||
Sliders:: |
constant | |||
Sliders:: |
constant | |||
Sliders:: |
public | function |
Form constructor. Overrides FilterWidgetBase:: |
|
Sliders:: |
public | function |
Gets default configuration for this plugin. Overrides FilterWidgetBase:: |
|
Sliders:: |
public | function |
Manipulate views exposed from element. Overrides FilterWidgetBase:: |
|
Sliders:: |
public static | function |
Verify this plugin can be used on the form element. Overrides FilterWidgetBase:: |
|
Sliders:: |
constant | |||
Sliders:: |
constant | |||
Sliders:: |
public | function |
Form validation handler. Overrides BetterExposedFiltersWidgetBase:: |
|
StringTranslationTrait:: |
protected | property | The string translation service. | 1 |
StringTranslationTrait:: |
protected | function | Formats a string containing a count of items. | |
StringTranslationTrait:: |
protected | function | Returns the number of plurals supported by a given language. | |
StringTranslationTrait:: |
protected | function | Gets the string translation service. | |
StringTranslationTrait:: |
public | function | Sets the string translation service to use. | 2 |
StringTranslationTrait:: |
protected | function | Translates a string to the current language or to a given language. |