View source
<?php
namespace Drupal\better_exposed_filters\Plugin\better_exposed_filters\filter;
use Drupal\better_exposed_filters\BetterExposedFiltersHelper;
use Drupal\Core\Form\FormStateInterface;
class RadioButtons extends FilterWidgetBase {
public function defaultConfiguration() {
return parent::defaultConfiguration() + [
'select_all_none' => FALSE,
'select_all_none_nested' => FALSE,
'display_inline' => FALSE,
];
}
public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
$filter = $this->handler;
$form = parent::buildConfigurationForm($form, $form_state);
$form['select_all_none'] = [
'#type' => 'checkbox',
'#title' => $this
->t('Add select all/none links'),
'#default_value' => !empty($this->configuration['select_all_none']),
'#disabled' => !$filter->options['expose']['multiple'],
'#description' => $this
->t('Add a "Select All/None" link when rendering the exposed filter using checkboxes. If this option is disabled, edit the filter and check the "Allow multiple selections".'),
];
$form['select_all_none_nested'] = [
'#type' => 'checkbox',
'#title' => $this
->t('Add nested all/none selection'),
'#default_value' => !empty($this->configuration['select_all_none_nested']),
'#disabled' => !$filter->options['expose']['multiple'] || isset($filter->options['hierarchy']) && !$filter->options['hierarchy'],
'#description' => $this
->t('When a parent checkbox is checked, check all its children. If this option is disabled, edit the filter and check "Allow multiple selections" and edit the filter settings and check "Show hierarchy in dropdown".'),
];
$form['display_inline'] = [
'#type' => 'checkbox',
'#title' => $this
->t('Display inline'),
'#default_value' => !empty($this->configuration['display_inline']),
'#description' => $this
->t('Display checkbox/radio options inline.'),
];
return $form;
}
public function exposedFormAlter(array &$form, FormStateInterface $form_state) {
$filter = $this->handler;
$field_id = $filter->options['is_grouped'] ? $filter->options['group_info']['identifier'] : $filter->options['expose']['identifier'];
parent::exposedFormAlter($form, $form_state);
if (!empty($form[$field_id])) {
if (!empty($form[$field_id]['#options'])) {
$form[$field_id]['#options'] = BetterExposedFiltersHelper::flattenOptions($form[$field_id]['#options']);
}
if (!empty($filter->options['hierarchy'])) {
$form[$field_id]['#bef_nested'] = TRUE;
}
$form[$field_id]['#bef_display_inline'] = $this->configuration['display_inline'];
if (!empty($form[$field_id]['#multiple'])) {
$form[$field_id]['#theme'] = 'bef_checkboxes';
$form[$field_id]['#type'] = 'checkboxes';
$form[$field_id]['#bef_select_all_none'] = $this->configuration['select_all_none'];
$form[$field_id]['#bef_select_all_none_nested'] = $this->configuration['select_all_none_nested'];
$form['#attached']['library'][] = 'better_exposed_filters/select_all_none';
}
else {
$form[$field_id]['#theme'] = 'bef_radios';
$form[$field_id]['#type'] = 'radios';
}
}
}
}