abstract class BetterExposedFiltersWidgetBase in Better Exposed Filters 8.5
Same name and namespace in other branches
- 8.4 src/Plugin/BetterExposedFiltersWidgetBase.php \Drupal\better_exposed_filters\Plugin\BetterExposedFiltersWidgetBase
Base class for Better exposed filters widget plugins.
Hierarchy
- class \Drupal\Component\Plugin\PluginBase implements DerivativeInspectionInterface, PluginInspectionInterface
- class \Drupal\better_exposed_filters\Plugin\BetterExposedFiltersWidgetBase implements BetterExposedFiltersWidgetInterface uses StringTranslationTrait
Expanded class hierarchy of BetterExposedFiltersWidgetBase
3 files declare their use of BetterExposedFiltersWidgetBase
- FilterWidgetBase.php in src/
Plugin/ better_exposed_filters/ filter/ FilterWidgetBase.php - PagerWidgetBase.php in src/
Plugin/ better_exposed_filters/ pager/ PagerWidgetBase.php - SortWidgetBase.php in src/
Plugin/ better_exposed_filters/ sort/ SortWidgetBase.php
File
- src/
Plugin/ BetterExposedFiltersWidgetBase.php, line 16
Namespace
Drupal\better_exposed_filters\PluginView source
abstract class BetterExposedFiltersWidgetBase extends PluginBase implements BetterExposedFiltersWidgetInterface {
use StringTranslationTrait;
/**
* The views executable object.
*
* @var \Drupal\views\ViewExecutable
*/
protected $view;
/**
* The views plugin this configuration will affect when exposed.
*
* @var \Drupal\views\Plugin\views\ViewsHandlerInterface
*/
protected $handler;
/**
* {@inheritdoc}
*/
public function __construct(array $configuration, $plugin_id, $plugin_definition) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->configuration = NestedArray::mergeDeep($this
->defaultConfiguration(), $configuration);
}
/**
* {@inheritdoc}
*/
public function defaultConfiguration() {
return [
'plugin_id' => $this->pluginId,
];
}
/**
* {@inheritdoc}
*/
public function getConfiguration() {
return $this->configuration;
}
/**
* {@inheritdoc}
*/
public function setConfiguration(array $configuration) {
$this->configuration = $configuration;
}
/**
* {@inheritdoc}
*/
public function setView(ViewExecutable $view) {
$this->view = $view;
}
/**
* {@inheritdoc}
*/
public function setViewsHandler(ViewsHandlerInterface $handler) {
$this->handler = $handler;
}
/**
* {@inheritdoc}
*/
public function validateConfigurationForm(array &$form, FormStateInterface $form_state) {
// Validation is optional.
}
/**
* {@inheritdoc}
*/
public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
// Apply submitted form state to configuration.
$values = $form_state
->getValues();
foreach ($values as $key => $value) {
if (array_key_exists($key, $this->configuration)) {
$this->configuration[$key] = $value;
}
else {
// Remove from form state.
unset($values[$key]);
}
}
}
/*
* Helper functions.
*/
/**
* Sets metadata on the form elements for easier processing.
*
* @param array $element
* The form element to apply the metadata to.
*
* @see ://www.drupal.org/project/drupal/issues/2511548
*/
protected function addContext(array &$element) {
$element['#context'] = [
'#plugin_type' => 'bef',
'#plugin_id' => $this->pluginId,
'#view_id' => $this->view
->id(),
'#display_id' => $this->view->current_display,
];
}
/**
* Moves an exposed form element into a field group.
*
* @param array $form
* An associative array containing the structure of the form.
* @param \Drupal\Core\Form\FormStateInterface $form_state
* Exposed views form state.
* @param string $element
* The key of the form element.
* @param string $group
* The name of the group element.
*
* @throws \Drupal\Component\Plugin\Exception\PluginException
* If the instance cannot be created, such as if the ID is invalid.
*/
protected function addElementToGroup(array &$form, FormStateInterface $form_state, $element, $group) {
// Ensure group is enabled.
$form[$group]['#access'] = TRUE;
// Add element to group.
$form[$element]['#group'] = $group;
// Persist state of collapsible field-sets with active elements.
if (empty($form[$group]['#open'])) {
// Use raw user input to determine if field-set should be open or closed.
$user_input = $form_state
->getUserInput()[$element] ?? [
0,
];
// Take multiple values into account.
if (!is_array($user_input)) {
$user_input = [
$user_input,
];
}
// Check if one or more values are set for our current element.
$options = $form[$element]['#options'] ?? [];
$default_value = $form[$element]['#default_value'] ?? key($options);
$has_values = array_reduce($user_input, function ($carry, $value) use ($form, $element, $default_value) {
return $carry || ($value === $default_value ? '' : $value || $default_value === 0);
}, FALSE);
if ($has_values) {
$form[$group]['#open'] = TRUE;
}
}
}
/**
* Returns exposed form action URL object.
*
* @param \Drupal\Core\Form\FormStateInterface $form_state
* Exposed views form state.
*
* @return \Drupal\Core\Url
* Url object.
*/
protected function getExposedFormActionUrl(FormStateInterface $form_state) {
/** @var \Drupal\views\ViewExecutable $view */
$view = $form_state
->get('view');
$display = $form_state
->get('display');
if (isset($display['display_options']['path'])) {
return Url::fromRoute(implode('.', [
'view',
$view
->id(),
$display['id'],
]));
}
$request = \Drupal::request();
$url = Url::createFromRequest(clone $request);
$url
->setAbsolute();
return $url;
}
}
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 default configuration for this plugin. Overrides ConfigurableInterface:: |
3 |
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:: |
|
BetterExposedFiltersWidgetInterface:: |
public | function | Manipulate views exposed from element. | 3 |
BetterExposedFiltersWidgetInterface:: |
public static | function | Verify this plugin can be used on the form element. | 3 |
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. | |
PluginFormInterface:: |
public | function | Form constructor. | 36 |
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. |