class Select2Widget in Select 2 8
Same name in this branch
- 8 src/Plugin/Field/FieldWidget/Select2Widget.php \Drupal\select2\Plugin\Field\FieldWidget\Select2Widget
- 8 modules/select2_facets/src/Plugin/facets/widget/Select2Widget.php \Drupal\select2_facets\Plugin\facets\widget\Select2Widget
The select2 widget.
Plugin annotation
@FacetsWidget(
id = "select2",
label = @Translation("Select2"),
description = @Translation("A configurable widget that shows a select2."),
)
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\select2_facets\Plugin\facets\widget\Select2Widget implements ContainerFactoryPluginInterface
- class \Drupal\facets\Widget\WidgetPluginBase implements WidgetPluginInterface
- class \Drupal\Core\Plugin\PluginBase uses DependencySerializationTrait, MessengerTrait, StringTranslationTrait
Expanded class hierarchy of Select2Widget
File
- modules/
select2_facets/ src/ Plugin/ facets/ widget/ Select2Widget.php, line 24
Namespace
Drupal\select2_facets\Plugin\facets\widgetView source
class Select2Widget extends WidgetPluginBase implements ContainerFactoryPluginInterface {
/**
* The current request.
*
* @var \Symfony\Component\HttpFoundation\Request
*/
protected $request;
/**
* The key-value store for entity_autocomplete.
*
* @var \Drupal\Core\KeyValueStore\KeyValueStoreInterface
*/
protected $keyValueStore;
/**
* {@inheritdoc}
*/
public function __construct(array $configuration, $plugin_id, $plugin_definition, Request $request, KeyValueStoreInterface $key_value_store) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->request = $request;
$this->keyValueStore = $key_value_store;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static($configuration, $plugin_id, $plugin_definition, $container
->get('request_stack')
->getCurrentRequest(), $container
->get('keyvalue')
->get('entity_autocomplete'));
}
/**
* {@inheritdoc}
*/
public function defaultConfiguration() {
return [
'autocomplete' => FALSE,
'match_operator' => 'CONTAINS',
'width' => '100%',
] + parent::defaultConfiguration();
}
/**
* {@inheritdoc}
*/
public function build(FacetInterface $facet) {
$this->facet = $facet;
$items = [];
$active_items = [];
foreach ($facet
->getResults() as $result) {
if (empty($result
->getUrl())) {
continue;
}
$count = $result
->getCount();
$this->showNumbers = $this
->getConfiguration()['show_numbers'] && $count !== NULL;
$items[$result
->getUrl()
->toString()] = $this->showNumbers ? sprintf('%s (%d)', $result
->getDisplayValue(), $result
->getCount()) : $result
->getDisplayValue();
if ($result
->isActive()) {
$active_items[] = $result
->getUrl()
->toString();
}
}
$element = [
'#type' => 'select2',
'#options' => $items,
'#required' => FALSE,
'#value' => $active_items,
'#multiple' => !$facet
->getShowOnlyOneResult(),
'#autocomplete' => $this
->getConfiguration()['autocomplete'],
'#name' => $facet
->getName(),
'#title' => $facet
->get('show_title') ? $facet
->getName() : '',
'#attributes' => [
'data-drupal-facet-id' => $facet
->id(),
'data-drupal-selector' => 'facet-' . $facet
->id(),
'class' => [
'js-facets-select2',
'js-facets-widget',
],
],
'#attached' => [
'library' => [
'select2_facets/drupal.select2_facets.select2-widget',
],
],
'#cache' => [
'contexts' => [
'url.path',
'url.query_args',
],
],
'#select2' => [
'width' => $this
->getConfiguration()['width'],
],
];
if ($element['#autocomplete']) {
$element['#autocomplete_route_callback'] = [
$this,
'processFacetAutocomplete',
];
}
return $element;
}
/**
* {@inheritdoc}
*/
public function buildConfigurationForm(array $form, FormStateInterface $form_state, FacetInterface $facet) {
$form = parent::buildConfigurationForm($form, $form_state, $facet);
$form['width'] = [
'#type' => 'textfield',
'#title' => $this
->t('Field width'),
'#default_value' => $this
->getConfiguration()['width'],
'#description' => $this
->t("Define a width for the select2 field. It can be either 'element', 'computedstyle', 'style', 'resolve' or any possible CSS unit. E.g. 500px, 50%, 200em. See the <a href='https://select2.org/appearance#container-width'>select2 documentation</a> for further explanations."),
'#required' => TRUE,
'#size' => '12',
'#pattern' => "([0-9]*\\.[0-9]+|[0-9]+)(cm|mm|in|px|pt|pc|em|ex|ch|rem|vm|vh|vmin|vmax|%)|element|computedstyle|style|resolve|auto|initial|inherit",
];
$form['autocomplete'] = [
'#type' => 'checkbox',
'#title' => $this
->t('Autocomplete'),
'#default_value' => $this
->getConfiguration()['autocomplete'],
'#description' => $this
->t('Options will be lazy loaded. This is recommended for lists with a lot of values.'),
];
$form['match_operator'] = [
'#type' => 'radios',
'#title' => $this
->t('Autocomplete matching'),
'#default_value' => $this
->getConfiguration()['match_operator'],
'#options' => $this
->getMatchOperatorOptions(),
'#description' => $this
->t('Select the method used to collect autocomplete suggestions. Note that <em>Contains</em> can cause performance issues on sites with thousands of entities.'),
'#states' => [
'visible' => [
':input[name$="widget_config[autocomplete]"]' => [
'checked' => TRUE,
],
],
],
];
return $form;
}
/**
* Returns the options for the match operator.
*
* @return array
* List of options.
*/
protected function getMatchOperatorOptions() {
return [
'STARTS_WITH' => $this
->t('Starts with'),
'CONTAINS' => $this
->t('Contains'),
];
}
/**
* Set the autocomplete route properties.
*
* @param array $element
* The render element.
*
* @return array
* The render element with autocomplete settings.
*/
public function processFacetAutocomplete(array &$element) {
$selection_settings = [
'path' => $this->request
->getUri(),
'match_operator' => $this
->getConfiguration()['match_operator'],
'show_numbers' => $this
->getConfiguration()['show_numbers'],
];
// Store the selection settings in the key/value store and pass a hashed key
// in the route parameters.
$data = serialize($selection_settings) . $this->facet
->getFacetSourceId() . $this->facet
->id();
$selection_settings_key = Crypt::hmacBase64($data, Settings::getHashSalt());
if (!$this->keyValueStore
->has($selection_settings_key)) {
$this->keyValueStore
->set($selection_settings_key, $selection_settings);
}
$element['#autocomplete_route_name'] = 'select2_facets.facet_autocomplete';
$element['#autocomplete_route_parameters'] = [
'facetsource_id' => $this->facet
->getFacetSourceId(),
'facet_id' => $this->facet
->id(),
'selection_settings_key' => $selection_settings_key,
];
return $element;
}
}
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. | |
Select2Widget:: |
protected | property | The key-value store for entity_autocomplete. | |
Select2Widget:: |
protected | property | The current request. | |
Select2Widget:: |
public | function |
Builds the facet widget for rendering. Overrides WidgetPluginBase:: |
|
Select2Widget:: |
public | function |
Provides a configuration form for this widget. Overrides WidgetPluginBase:: |
|
Select2Widget:: |
public static | function |
Creates an instance of the plugin. Overrides ContainerFactoryPluginInterface:: |
|
Select2Widget:: |
public | function |
Gets default configuration for this plugin. Overrides WidgetPluginBase:: |
|
Select2Widget:: |
protected | function | Returns the options for the match operator. | |
Select2Widget:: |
public | function | Set the autocomplete route properties. | |
Select2Widget:: |
public | function |
Constructs a \Drupal\Component\Plugin\PluginBase object. Overrides WidgetPluginBase:: |
|
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:: |
public | function |
Checks is a specific property is required for this widget. Overrides WidgetPluginInterface:: |
2 |
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 |