Setting value for a views block exposed filter is broken if filter has custom name

Created on 22 May 2023, about 1 year ago
Updated 14 June 2024, 15 days ago

Problem/Motivation

While the selected value is correctly set in the block config on save and will apply to the front end, when reopening the block to edit, the filter configuration is lost as the code uses the wrong key to attempt to set the value from the stored config.

The problem is that the loop uses the id of the filter from the array NOT the identifier of the exposed filter. By default these would be the same thing but if you have customised the name in the view then the code fails.

Steps to reproduce

Create a View display for a Block with exposed filters that you customise the name for, instead of it defaulting to the field name for example. Set the filters to be allowed settings in the block configuration.
Create a new block and then set the filter configuration and save the block.
Open the block to edit again and the selected values will not be reset. This will cause the initial config to be lost when editing the block.

Proposed resolution

Use the identifier from the exposed filter. This covers both when the default value or a custom value is used and the config is correctly displayed on later edits.

diff --git a/modules/ctools_views/src/Plugin/Display/Block.php b/modules/ctools_views/src/Plugin/Display/Block.php
index 00230e82..3273cf4f 100644
--- a/modules/ctools_views/src/Plugin/Display/Block.php
+++ b/modules/ctools_views/src/Plugin/Display/Block.php
@@ -296,11 +296,11 @@ class Block extends CoreBlock {
           }
           else {
             $handler->buildExposedForm($form['exposed'][$filter_key], $exposed_form_state);
-
-            $form_field_present = isset($form['exposed'][$filter_key][$id]);
+            $identifier = $handler->options['expose']['identifier'];
+            $form_field_present = isset($form['exposed'][$filter_key][$identifier]);
             $block_config_present = isset($block_configuration['exposed'][$filter_key]);
 
-            $form_field_type = $form_field_present ? $form['exposed'][$filter_key][$id]['#type'] : FALSE;
+            $form_field_type = $form_field_present ? $form['exposed'][$filter_key][$identifier]['#type'] : FALSE;
             $filter_plugin_id = $block_config_present ? $block_configuration['exposed'][$filter_key]['plugin_id'] : FALSE;
             if ($form_field_present && $block_config_present) {
 
@@ -308,14 +308,14 @@ class Block extends CoreBlock {
                 // Single-value select elements get their default value set to
                 // 'All' in buildExposedForm(), when that option is added, so set
                 // thir defaults manually.
-                $form['exposed'][$filter_key][$id]['#default_value'] = $block_configuration['exposed'][$filter_key]['value'];
+                $form['exposed'][$filter_key][$identifier]['#default_value'] = $block_configuration['exposed'][$filter_key]['value'];
               }
 
               else if ($form_field_type =='entity_autocomplete' && $filter_plugin_id == 'taxonomy_index_tid') {
                 // Entity reference autocomplete fields need their values
                 // converted back to a string for the textfield input.
                 $terms = \Drupal::entityTypeManager()->getStorage('taxonomy_term')->loadMultiple($block_configuration['exposed'][$filter_key]['value']);
-                $form['exposed'][$filter_key][$id]['#default_value'] = EntityAutocomplete::getEntityLabels($terms);
+                $form['exposed'][$filter_key][$identifier]['#default_value'] = EntityAutocomplete::getEntityLabels($terms);
               }
             }
           }

Cheers, Neil

πŸ› Bug report
Status

Needs review

Version

4.1

Component

Code

Created by

πŸ‡¬πŸ‡§United Kingdom altcom_nh

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Production build 0.69.0 2024