class SliderWidget in Facets 8
The slider widget.
Plugin annotation
@FacetsWidget(
id = "slider",
label = @Translation("Slider"),
description = @Translation("A widget that shows a slider."),
)
Hierarchy
- class \Drupal\Component\Plugin\PluginBase implements DerivativeInspectionInterface, PluginInspectionInterface
- class \Drupal\Core\Plugin\PluginBase uses DependencySerializationTrait, MessengerTrait, StringTranslationTrait
- class \Drupal\facets\Widget\WidgetPluginBase implements WidgetPluginInterface
- class \Drupal\facets_range_widget\Plugin\facets\widget\SliderWidget
- class \Drupal\facets\Widget\WidgetPluginBase implements WidgetPluginInterface
- class \Drupal\Core\Plugin\PluginBase uses DependencySerializationTrait, MessengerTrait, StringTranslationTrait
Expanded class hierarchy of SliderWidget
1 file declares its use of SliderWidget
- SliderWidgetTest.php in modules/
facets_range_widget/ tests/ src/ Unit/ Plugin/ widget/ SliderWidgetTest.php
File
- modules/
facets_range_widget/ src/ Plugin/ facets/ widget/ SliderWidget.php, line 18
Namespace
Drupal\facets_range_widget\Plugin\facets\widgetView source
class SliderWidget extends WidgetPluginBase {
/**
* {@inheritdoc}
*/
public function defaultConfiguration() {
return [
'prefix' => '',
'suffix' => '',
'min_type' => 'search_result',
'min_value' => 0,
'max_type' => 'search_result',
'max_value' => 10,
'step' => 1,
] + parent::defaultConfiguration();
}
/**
* {@inheritdoc}
*/
public function build(FacetInterface $facet) {
$build = parent::build($facet);
$results = $facet
->getResults();
if (empty($results)) {
return $build;
}
ksort($results);
$show_numbers = $facet
->getWidgetInstance()
->getConfiguration()['show_numbers'];
$urls = [];
$labels = [];
foreach ($results as $result) {
$urls['f_' . $result
->getRawValue()] = $result
->getUrl()
->toString();
$labels[] = $result
->getDisplayValue() . ($show_numbers ? ' (' . $result
->getCount() . ')' : '');
}
// The results set on the facet are sorted where the minimum is the first
// item and the last one is the one with the highest results, so it's safe
// to use min/max.
$min = (double) reset($results)
->getRawValue();
$max = (double) end($results)
->getRawValue();
$build['#items'] = [
[
'#type' => 'html_tag',
'#tag' => 'div',
'#attributes' => [
'class' => [
'facet-slider',
],
'id' => $facet
->id(),
],
],
];
$active = $facet
->getActiveItems();
$build['#attached']['library'][] = 'facets_range_widget/slider';
$build['#attached']['drupalSettings']['facets']['sliders'][$facet
->id()] = [
'min' => $min,
'max' => $max,
'value' => isset($active[0]) ? (double) $active[0] : '',
'urls' => $urls,
'prefix' => $this
->getConfiguration()['prefix'],
'suffix' => $this
->getConfiguration()['suffix'],
'step' => $this
->getConfiguration()['step'],
'labels' => $labels,
];
return $build;
}
/**
* {@inheritdoc}
*/
public function buildConfigurationForm(array $form, FormStateInterface $form_state, FacetInterface $facet) {
$config = $this
->getConfiguration();
$form = parent::buildConfigurationForm($form, $form_state, $facet);
$form['prefix'] = [
'#type' => 'textfield',
'#title' => $this
->t('Value prefix'),
'#size' => 5,
'#default_value' => $config['prefix'],
];
$form['suffix'] = [
'#type' => 'textfield',
'#title' => $this
->t('Value suffix'),
'#size' => 5,
'#default_value' => $config['suffix'],
];
$form['min_type'] = [
'#type' => 'radios',
'#options' => [
'fixed' => $this
->t('Fixed value'),
'search_result' => $this
->t('Based on search result'),
],
'#title' => $this
->t('Minimum value type'),
'#default_value' => $config['min_type'],
];
$form['min_value'] = [
'#type' => 'number',
'#title' => $this
->t('Minimum value'),
'#default_value' => $config['min_value'],
'#size' => 10,
'#states' => [
'visible' => [
'input[name="widget_config[min_type]"]' => [
'value' => 'fixed',
],
],
],
];
$form['max_type'] = [
'#type' => 'radios',
'#options' => [
'fixed' => $this
->t('Fixed value'),
'search_result' => $this
->t('Based on search result'),
],
'#title' => $this
->t('Maximum value type'),
'#default_value' => $config['max_type'],
];
$form['max_value'] = [
'#type' => 'number',
'#title' => $this
->t('Maximum value'),
'#default_value' => $config['max_value'],
'#size' => 5,
'#states' => [
'visible' => [
'input[name="widget_config[max_type]"]' => [
'value' => 'fixed',
],
],
],
];
$form['step'] = [
'#type' => 'number',
'#step' => 0.001,
'#title' => $this
->t('slider step'),
'#default_value' => $config['step'],
'#size' => 2,
];
return $form;
}
/**
* {@inheritdoc}
*/
public function isPropertyRequired($name, $type) {
if ($name === 'slider' && $type === 'processors') {
return TRUE;
}
if ($name === 'show_only_one_result' && $type === 'settings') {
return TRUE;
}
return FALSE;
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
DependencySerializationTrait:: |
protected | property | An array of entity type IDs keyed by the property name of their storages. | |
DependencySerializationTrait:: |
protected | property | An array of service IDs keyed by property name used for serialization. | |
DependencySerializationTrait:: |
public | function | 1 | |
DependencySerializationTrait:: |
public | function | 2 | |
MessengerTrait:: |
protected | property | The messenger. | 29 |
MessengerTrait:: |
public | function | Gets the messenger. | 29 |
MessengerTrait:: |
public | function | Sets the messenger. | |
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. | |
SliderWidget:: |
public | function |
Builds the facet widget for rendering. Overrides WidgetPluginBase:: |
1 |
SliderWidget:: |
public | function |
Provides a configuration form for this widget. Overrides WidgetPluginBase:: |
|
SliderWidget:: |
public | function |
Gets default configuration for this plugin. Overrides WidgetPluginBase:: |
|
SliderWidget:: |
public | function |
Checks is a specific property is required for this widget. Overrides WidgetPluginBase:: |
1 |
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. | |
WidgetPluginBase:: |
protected | property | The facet the widget is being built for. | |
WidgetPluginBase:: |
protected | property | Show the amount of results next to the result. | |
WidgetPluginBase:: |
protected | function | Builds a renderable array of result items. | 1 |
WidgetPluginBase:: |
protected | function | Builds a facet result item. | |
WidgetPluginBase:: |
public | function |
Calculates dependencies for the configured plugin. Overrides DependentPluginInterface:: |
|
WidgetPluginBase:: |
public | function |
Gets this plugin's configuration. Overrides ConfigurableInterface:: |
|
WidgetPluginBase:: |
protected | function | Provides a full array of possible theme functions to try for a given hook. | |
WidgetPluginBase:: |
public | function |
Picks the preferred query type for this widget. Overrides WidgetPluginInterface:: |
3 |
WidgetPluginBase:: |
protected | function | Returns the text or link for an item. | |
WidgetPluginBase:: |
public | function |
Sets the configuration for this plugin instance. Overrides ConfigurableInterface:: |
|
WidgetPluginBase:: |
public | function |
Checks if the facet is supported by this processor. Overrides WidgetPluginInterface:: |
1 |
WidgetPluginBase:: |
public | function |
Constructs a \Drupal\Component\Plugin\PluginBase object. Overrides PluginBase:: |