You are here

class Sliders in Better Exposed Filters 8.4

Same name and namespace in other branches
  1. 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

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\filter
View 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

Namesort descending Modifiers Type Description Overrides
BetterExposedFiltersWidgetBase::$handler protected property The views plugin this configuration will affect when exposed.
BetterExposedFiltersWidgetBase::$view protected property The views executable object.
BetterExposedFiltersWidgetBase::addContext protected function Sets metadata on the form elements for easier processing.
BetterExposedFiltersWidgetBase::addElementToGroup protected function Moves an exposed form element into a field group.
BetterExposedFiltersWidgetBase::getConfiguration public function Gets this plugin's configuration. Overrides ConfigurableInterface::getConfiguration
BetterExposedFiltersWidgetBase::getExposedFormActionUrl protected function Returns exposed form action URL object.
BetterExposedFiltersWidgetBase::setConfiguration public function Sets the configuration for this plugin instance. Overrides ConfigurableInterface::setConfiguration
BetterExposedFiltersWidgetBase::setView public function Sets the view object. Overrides BetterExposedFiltersWidgetInterface::setView
BetterExposedFiltersWidgetBase::setViewsHandler public function Sets the exposed view handler plugin. Overrides BetterExposedFiltersWidgetInterface::setViewsHandler
BetterExposedFiltersWidgetBase::submitConfigurationForm public function Form submission handler. Overrides PluginFormInterface::submitConfigurationForm
BetterExposedFiltersWidgetBase::__construct public function Constructs a \Drupal\Component\Plugin\PluginBase object. Overrides PluginBase::__construct
FilterWidgetBase::getExposedFilterFieldId protected function Helper function to get the unique identifier for the exposed filter.
FilterWidgetBase::getExposedFilterWidgetType protected function Helper function to get the widget type of the exposed filter.
FilterWidgetBase::processSortedOptions public function Sorts the options for a given form element alphabetically.
PluginBase::$configuration protected property Configuration information passed into the plugin. 1
PluginBase::$pluginDefinition protected property The plugin implementation definition. 1
PluginBase::$pluginId protected property The plugin_id.
PluginBase::DERIVATIVE_SEPARATOR constant A string which is used to separate base plugin IDs from the derivative ID.
PluginBase::getBaseId public function Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface::getBaseId
PluginBase::getDerivativeId public function Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface::getDerivativeId
PluginBase::getPluginDefinition public function Gets the definition of the plugin implementation. Overrides PluginInspectionInterface::getPluginDefinition 3
PluginBase::getPluginId public function Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface::getPluginId
PluginBase::isConfigurable public function Determines if the plugin is configurable.
Sliders::ANIMATE_CUSTOM constant
Sliders::ANIMATE_FAST constant
Sliders::ANIMATE_NONE constant
Sliders::ANIMATE_NORMAL constant
Sliders::ANIMATE_SLOW constant
Sliders::buildConfigurationForm public function Form constructor. Overrides FilterWidgetBase::buildConfigurationForm
Sliders::defaultConfiguration public function Gets default configuration for this plugin. Overrides FilterWidgetBase::defaultConfiguration
Sliders::exposedFormAlter public function Manipulate views exposed from element. Overrides FilterWidgetBase::exposedFormAlter
Sliders::isApplicable public static function Verify this plugin can be used on the form element. Overrides FilterWidgetBase::isApplicable
Sliders::ORIENTATION_HORIZONTAL constant
Sliders::ORIENTATION_VERTICAL constant
Sliders::validateConfigurationForm public function Form validation handler. Overrides BetterExposedFiltersWidgetBase::validateConfigurationForm
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.