From 25a2823e1051510ce26fec34bc4e97199c35a3b3 Mon Sep 17 00:00:00 2001
From: Lee Rowlands <lee.rowlands@previousnext.com.au>
Date: Tue, 12 Jul 2022 08:04:22 +1000
Subject: [PATCH] Issue #2916682 by Matroskeen: Defaults not working for
 grouped filter with multiple selections

---
 .../Plugin/views/filter/FilterPluginBase.php  |  2 +-
 .../src/Functional/ExposedFormUITest.php      | 32 ++++++++++++++++++-
 2 files changed, 32 insertions(+), 2 deletions(-)

diff --git a/core/modules/views/src/Plugin/views/filter/FilterPluginBase.php b/core/modules/views/src/Plugin/views/filter/FilterPluginBase.php
index b048d8e281f3..6401fe01435f 100644
--- a/core/modules/views/src/Plugin/views/filter/FilterPluginBase.php
+++ b/core/modules/views/src/Plugin/views/filter/FilterPluginBase.php
@@ -882,7 +882,7 @@ public function groupForm(&$form, FormStateInterface $form_state) {
         }
         unset($form[$value]['#default_value']);
         $user_input = $form_state->getUserInput();
-        if (empty($user_input)) {
+        if (empty($user_input[$value])) {
           $user_input[$value] = $this->group_info;
           $form_state->setUserInput($user_input);
         }
diff --git a/core/modules/views_ui/tests/src/Functional/ExposedFormUITest.php b/core/modules/views_ui/tests/src/Functional/ExposedFormUITest.php
index f1f959958ea8..85f66cd1fd41 100644
--- a/core/modules/views_ui/tests/src/Functional/ExposedFormUITest.php
+++ b/core/modules/views_ui/tests/src/Functional/ExposedFormUITest.php
@@ -324,12 +324,27 @@ public function testExposedGroupedFilter() {
       'options[group_info][group_items][2][value][article]' => 'article',
       'options[group_info][group_items][3][title]' => '3rd',
       'options[group_info][group_items][3][value][page]' => 'page',
+      'options[group_info][default_group]' => '3',
     ];
     // Apply the filter settings.
     $this->submitForm($edit, 'Apply');
     // Check that the view is saved without errors.
     $this->submitForm([], 'Save');
     $this->assertSession()->statusCodeEquals(200);
+    // Check the default filter value.
+    $this->drupalGet('test_exposed_admin_ui');
+    $this->assertSession()->fieldValueEquals('type', '3');
+    // Enable "Allow multiple selections" option and set a default group.
+    $this->drupalGet('admin/structure/views/nojs/handler/test_exposed_admin_ui/default/filter/type');
+    $edit['options[group_info][multiple]'] = 1;
+    $edit['options[group_info][default_group_multiple][1]'] = 1;
+    $this->submitForm($edit, 'Apply');
+    $this->submitForm([], 'Save');
+    // Check the default filter values again.
+    $this->drupalGet('test_exposed_admin_ui');
+    $this->assertSession()->checkboxChecked('type[1]');
+    $this->assertSession()->checkboxNotChecked('type[2]');
+    $this->assertSession()->checkboxNotChecked('type[3]');
 
     // Click the Expose filter button.
     $this->drupalGet('admin/structure/views/nojs/add-handler/test_exposed_admin_ui/default/filter');
@@ -348,18 +363,33 @@ public function testExposedGroupedFilter() {
       'options[group_info][group_items][2][value]' => 1,
       'options[group_info][group_items][3][title]' => 'Unpublished',
       'options[group_info][group_items][3][value]' => 0,
+      'options[group_info][default_group]' => 2,
     ];
     // Apply the filter settings.
     $this->submitForm($edit, 'Apply');
     // Check that the view is saved without errors.
     $this->submitForm([], 'Save');
     $this->assertSession()->statusCodeEquals(200);
-
     $this->drupalGet('admin/structure/views/nojs/handler/test_exposed_admin_ui/default/filter/status');
     // Assert the same settings defined before still are there.
     $this->assertSession()->checkboxChecked('edit-options-group-info-group-items-1-value-all');
     $this->assertSession()->checkboxChecked('edit-options-group-info-group-items-2-value-1');
     $this->assertSession()->checkboxChecked('edit-options-group-info-group-items-3-value-0');
+
+    // Check the default filter value.
+    $this->drupalGet('test_exposed_admin_ui');
+    $this->assertSession()->fieldValueEquals('status', '2');
+    // Enable "Allow multiple selections" option and set a default group.
+    $this->drupalGet('admin/structure/views/nojs/handler/test_exposed_admin_ui/default/filter/status');
+    $edit['options[group_info][multiple]'] = 1;
+    $edit['options[group_info][default_group_multiple][3]'] = 1;
+    $this->submitForm($edit, 'Apply');
+    $this->submitForm([], 'Save');
+    // Check the default filter value again.
+    $this->drupalGet('test_exposed_admin_ui');
+    $this->assertSession()->checkboxNotChecked('status[1]');
+    $this->assertSession()->checkboxNotChecked('status[2]');
+    $this->assertSession()->checkboxChecked('status[3]');
   }
 
 }
-- 
GitLab