class Single in Better Exposed Filters 8.5
Same name and namespace in other branches
- 8.4 src/Plugin/better_exposed_filters/filter/Single.php \Drupal\better_exposed_filters\Plugin\better_exposed_filters\filter\Single
Single on/off widget implementation.
Plugin annotation
@BetterExposedFiltersFilterWidget(
id = "bef_single",
label = @Translation("Single On/Off Checkbox"),
)
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\Single
- 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 Single
File
- src/
Plugin/ better_exposed_filters/ filter/ Single.php, line 15
Namespace
Drupal\better_exposed_filters\Plugin\better_exposed_filters\filterView source
class Single extends FilterWidgetBase {
/**
* {@inheritdoc}
*/
public static function isApplicable($filter = NULL, array $filter_options = []) {
/** @var \Drupal\views\Plugin\views\filter\FilterPluginBase $filter */
$is_applicable = FALSE;
// Sanity check to ensure we have a filter to work with.
if (!isset($filter)) {
return $is_applicable;
}
if (is_a($filter, 'Drupal\\views\\Plugin\\views\\filter\\BooleanOperator') || $filter
->isAGroup() && count($filter->options['group_info']['group_items']) == 1) {
$is_applicable = TRUE;
}
return $is_applicable;
}
/**
* {@inheritdoc}
*/
public function exposedFormAlter(array &$form, FormStateInterface $form_state) {
/** @var \Drupal\views\Plugin\views\filter\FilterPluginBase $filter */
$filter = $this->handler;
// Form element is designated by the element ID which is user-
// configurable, and stored differently for grouped filters.
$exposed_id = $filter->options['expose']['identifier'];
$field_id = $this
->getExposedFilterFieldId();
parent::exposedFormAlter($form, $form_state);
if (!empty($form[$field_id])) {
// Views populates missing values in $form_state['input'] with the
// defaults and a checkbox does not appear in $_GET (or $_POST) so it
// will appear to be missing when a user submits a form. Because of
// this, instead of unchecking the checkbox value will revert to the
// default. More, the default value for select values (i.e. 'Any') is
// reused which results in the checkbox always checked.
$input = $form_state
->getUserInput();
// The input value ID is not always consistent.
// Prioritize the field ID, but default to exposed ID.
// @todo Remove $exposed_id once
// https://www.drupal.org/project/drupal/issues/288429 is fixed.
$input_value = isset($input[$field_id]) ? $input[$field_id] : (isset($input[$exposed_id]) ? $input[$exposed_id] : NULL);
$checked = FALSE;
// We need to be super careful when working with raw input values. Let's
// make sure the value exists in our list of possible options.
if (in_array($input_value, array_keys($form[$field_id]['#options'])) && $input_value !== 'All') {
$checked = (bool) $input_value;
}
$form[$field_id]['#type'] = 'checkbox';
$form[$field_id]['#default_value'] = 0;
$form[$field_id]['#return_value'] = 1;
$form[$field_id]['#value'] = $checked ? 1 : 0;
}
}
}
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 |
Form validation handler. Overrides PluginFormInterface:: |
1 |
BetterExposedFiltersWidgetBase:: |
public | function |
Constructs a \Drupal\Component\Plugin\PluginBase object. Overrides PluginBase:: |
|
FilterWidgetBase:: |
public | function |
Form constructor. Overrides PluginFormInterface:: |
3 |
FilterWidgetBase:: |
public | function |
Gets default configuration for this plugin. Overrides BetterExposedFiltersWidgetBase:: |
3 |
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. | |
Single:: |
public | function |
Manipulate views exposed from element. Overrides FilterWidgetBase:: |
|
Single:: |
public static | function |
Verify this plugin can be used on the form element. Overrides FilterWidgetBase:: |
|
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. |