Commit 2b42e73a authored by Etroid's avatar Etroid
Browse files

Issue #3131580 by Etroid, JD_1: Chosen is no longer applied after update to BEF 4

parent 7c3ead39
......@@ -164,26 +164,6 @@ abstract class BetterExposedFiltersWidgetBase extends PluginBase implements Bett
$form[$group]['#open'] = TRUE;
}
}
/** @var \Drupal\Core\Render\ElementInfoManager $element_info_manager */
$element_info_manager = \Drupal::service('plugin.manager.element_info');
$element_info_plugin = $element_info_manager->createInstance($form[$element]['#type']);
$element_info = $element_info_plugin->getInfo();
if (!empty($element_info['#process'])) {
foreach ($element_info['#process'] as $process) {
if (is_array($process) && $process[1] === 'processGroup') {
// This element already supports #group.
return;
}
}
}
// Workaround to add support for #group FAPI to all elements currently not
// supported.
// @todo remove once core issue is resolved.
// @see https://www.drupal.org/project/drupal/issues/2190333
$form[$element]['#process'][] = ['\Drupal\Core\Render\Element\RenderElement', 'processGroup'];
$form[$element]['#pre_render'][] = ['\Drupal\Core\Render\Element\RenderElement', 'preRenderGroup'];
}
/**
......
......@@ -21,24 +21,4 @@ class DefaultWidget extends FilterWidgetBase {
return TRUE;
}
/**
* {@inheritdoc}
*/
public function exposedFormAlter(array &$form, FormStateInterface $form_state) {
$field_id = $this->getExposedFilterFieldId();
$type = $this->getExposedFilterWidgetType();
parent::exposedFormAlter($form, $form_state);
if ($type === 'select') {
// Workaround to add support for merging process and pre-render functions
// to the render array of an element.
// @todo remove once core issue is resolved.
// @see https://www.drupal.org/project/drupal/issues/2070131
$form[$field_id]['#process'][] = ['\Drupal\Core\Render\Element\Select', 'processSelect'];
$form[$field_id]['#process'][] = ['\Drupal\Core\Render\Element\Select', 'processAjaxForm'];
$form[$field_id]['#pre_render'][] = ['\Drupal\Core\Render\Element\Select', 'preRenderSelect'];
}
}
}
......@@ -172,7 +172,7 @@ abstract class FilterWidgetBase extends BetterExposedFiltersWidgetBase implement
if ($this->configuration['advanced']['sort_options']) {
$form[$field_id]['#nested'] = $filter->options['hierarchy'] ?? FALSE;
$form[$field_id]['#nested_delimiter'] = '-';
$form[$field_id]['#process'][] = [$this, 'processSortedOptions'];
$form[$field_id]['#pre_process'][] = [$this, 'processSortedOptions'];
}
// Check for placeholder text.
......@@ -325,9 +325,6 @@ abstract class FilterWidgetBase extends BetterExposedFiltersWidgetBase implement
$filter = $this->handler;
$filter_id = $filter->options['expose']['identifier'];
/** @var \Drupal\views\Plugin\views\filter\FilterPluginBase $filter */
$filter->buildExposedForm($form, $form_state);
return $form[$filter_id]['#type'] ?? $form[$filter_id]['value']['#type'] ?? '';
}
......
......@@ -95,7 +95,6 @@ class RadioButtons extends FilterWidgetBase {
if (!empty($form[$field_id]['#multiple'])) {
$form[$field_id]['#theme'] = 'bef_checkboxes';
$form[$field_id]['#type'] = 'checkboxes';
$form[$field_id]['#process'][] = ['\Drupal\Core\Render\Element\Checkboxes', 'processCheckboxes'];
// Show all/none option.
$form[$field_id]['#bef_select_all_none'] = $this->configuration['select_all_none'];
......@@ -108,7 +107,6 @@ class RadioButtons extends FilterWidgetBase {
else {
$form[$field_id]['#theme'] = 'bef_radios';
$form[$field_id]['#type'] = 'radios';
$form[$field_id]['#process'][] = ['\Drupal\Core\Render\Element\Radios', 'processRadios'];
}
}
}
......
......@@ -69,7 +69,6 @@ class Single extends FilterWidgetBase {
$form[$field_id]['#default_value'] = 0;
$form[$field_id]['#return_value'] = 1;
$form[$field_id]['#value'] = $checked ? 1 : 0;
$form[$field_id]['#process'][] = ['\Drupal\Core\Render\Element\Checkbox', 'processCheckbox'];
}
}
......
......@@ -22,10 +22,6 @@ class RadioButtons extends PagerWidgetBase {
if (!empty($form['items_per_page'])) {
$form['items_per_page']['#type'] = 'radios';
if (empty($form['items_per_page']['#process'])) {
$form['items_per_page']['#process'] = [];
}
array_unshift($form['items_per_page']['#process'], ['\Drupal\Core\Render\Element\Radios', 'processRadios']);
$form['items_per_page']['#prefix'] = '<div class="bef-sortby bef-select-as-radios">';
$form['items_per_page']['#suffix'] = '</div>';
}
......
......@@ -24,10 +24,6 @@ class RadioButtons extends SortWidgetBase {
if (!empty($form[$element])) {
$form[$element]['#theme'] = 'bef_radios';
$form[$element]['#type'] = 'radios';
if (empty($form[$element]['#process'])) {
$form[$element]['#process'] = [];
}
$form[$element]['#process'][] = ['\Drupal\Core\Render\Element\Radios', 'processRadios'];
}
}
}
......
......@@ -690,6 +690,9 @@ class BetterExposedFilters extends InputRequired {
public function exposedFormAlter(&$form, FormStateInterface $form_state) {
parent::exposedFormAlter($form, $form_state);
// Mark form as Better Exposed Filter form for easier alterations.
$form['#context']['bef'] = TRUE;
// These styles are used on all exposed forms.
$form['#attached']['library'][] = 'better_exposed_filters/general';
......@@ -805,6 +808,13 @@ class BetterExposedFilters extends InputRequired {
$has_visible_filters = !empty(Element::getVisibleChildren($form)) ?: FALSE;
$form['actions']['submit']['#access'] = $has_visible_filters;
$form['actions']['reset']['#access'] = $has_visible_filters;
// Ensure default process/pre_render callbacks are included when a BEF
// widget has added their own.
foreach (Element::children($form) as $key) {
$element = &$form[$key];
$this->addDefaultElementInfo($element);
}
}
/**
......@@ -841,4 +851,53 @@ class BetterExposedFilters extends InputRequired {
return array_splice($form, 0, $pos - 1) + $element + $form;
}
/**
* Adds default element callbacks.
*
* This is a workaround where adding process and pre-render functions are not
* results in replacing the default ones instead of merging.
*
* @param array $element
* The render array for a single form element.
*
* @todo remove once the following issues are resolved.
* @see https://www.drupal.org/project/drupal/issues/2070131
* @see https://www.drupal.org/project/drupal/issues/2190333
*/
protected function addDefaultElementInfo(array &$element) {
/** @var \Drupal\Core\Render\ElementInfoManager $element_info_manager */
$element_info = \Drupal::service('element_info');
if (isset($element['#type']) && empty($element['#defaults_loaded']) && ($info = $element_info->getInfo($element['#type']))) {
$element['#process'] = $element['#process'] ?? [];
$element['#pre_render'] = $element['#pre_render'] ?? [];
if (!empty($info['#process'])) {
$element['#process'] = array_merge($info['#process'], $element['#process']);
}
if (!empty($info['#pre_render'])) {
$element['#pre_render'] = array_merge($info['#pre_render'], $element['#pre_render']);
}
// Some processing needs to happen prior to the default form element
// callbacks (e.g. sort). We use the custom '#pre_process' array for this.
if (!empty($element['#pre_process'])) {
$element['#process'] = array_merge($element['#pre_process'], $element['#process']);
}
// Workaround to add support for #group FAPI to all elements currently not
// supported.
// @todo remove once core issue is resolved.
// @see https://www.drupal.org/project/drupal/issues/2190333
if (!in_array('processGroup', array_column($element['#process'], 1))) {
$element['#process'][] = ['\Drupal\Core\Render\Element\RenderElement', 'processGroup'];
$element['#pre_render'][] = ['\Drupal\Core\Render\Element\RenderElement', 'preRenderGroup'];
}
}
// Apply the same to any nested children.
foreach (Element::children($element) as $key) {
$child = &$element[$key];
$this->addDefaultElementInfo($child);
}
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment