diff --git a/core/modules/views/src/ViewExecutable.php b/core/modules/views/src/ViewExecutable.php index 84049aec1f0bb909b1c4dcb2660d8b8478f9d5b2..ff88e497430f8b188c74aa8531147d26a49d0c17 100644 --- a/core/modules/views/src/ViewExecutable.php +++ b/core/modules/views/src/ViewExecutable.php @@ -712,15 +712,16 @@ public function getExposedInput() { } // If we have no input at all, check for remembered input via session. - - // If filters are not overridden, store the 'remember' settings on the - // default display. If they are, store them on this display. This way, - // multiple displays in the same view can share the same filters and - // remember settings. - $display_id = ($this->display_handler->isDefaulted('filters')) ? 'default' : $this->current_display; - - if (empty($this->exposed_input) && !empty($_SESSION['views'][$this->storage->id()][$display_id])) { - $this->exposed_input = $_SESSION['views'][$this->storage->id()][$display_id]; + if (empty($this->exposed_input) && $this->request->hasSession()) { + $session = \Drupal::request()->getSession(); + // If filters are not overridden, store the 'remember' settings on the + // default display. If they are, store them on this display. This way, + // multiple displays in the same view can share the same filters and + // remember settings. + $display_id = ($this->display_handler->isDefaulted('filters')) ? 'default' : $this->current_display; + if (!empty($session->get('views')[$this->storage->id()][$display_id])) { + $this->exposed_input = $session->get('views')[$this->storage->id()][$display_id]; + } } } diff --git a/core/modules/views/tests/modules/views_test_config/test_views/views.view.test_remember_selected.yml b/core/modules/views/tests/modules/views_test_config/test_views/views.view.test_remember_selected.yml new file mode 100644 index 0000000000000000000000000000000000000000..8ae1034ab0da67f3420a298dc004498d5e3556bb --- /dev/null +++ b/core/modules/views/tests/modules/views_test_config/test_views/views.view.test_remember_selected.yml @@ -0,0 +1,110 @@ +langcode: en +status: true +dependencies: + config: + - core.entity_view_mode.node.teaser + module: + - node +id: test_remember_selected +label: '' +module: views +description: '' +tag: '' +base_table: node_field_data +base_field: nid +display: + default: + id: default + display_title: Default + display_plugin: default + position: 0 + display_options: + pager: + type: full + exposed_form: + type: basic + options: + reset_button: true + access: + type: none + cache: + type: none + options: { } + filters: + type: + id: type + table: node_field_data + field: type + relationship: none + group_type: group + admin_label: '' + entity_type: node + entity_field: type + plugin_id: in_operator + operator: in + value: { } + group: 1 + exposed: true + expose: + operator_id: type_op + label: 'Content: Type' + description: 'Exposed description' + use_operator: false + operator: '' + operator_limit_selection: false + operator_list: { } + identifier: type + required: false + remember: true + multiple: false + remember_roles: + anonymous: anonymous + authenticated: authenticated + content_editor: '0' + administrator: '0' + reduce: false + is_grouped: false + group_info: + label: '' + description: '' + identifier: '' + optional: true + widget: select + multiple: false + remember: false + default_group: All + default_group_multiple: { } + group_items: { } + style: + type: default + row: + type: 'entity:node' + query: + type: views_query + options: + query_comment: '' + display_extenders: { } + cache_metadata: + max-age: -1 + contexts: + - 'languages:language_interface' + - url + - url.query_args + - 'user.node_grants:view' + tags: { } + page_1: + id: page_1 + display_title: Page + display_plugin: page + position: 0 + display_options: + display_extenders: { } + path: test_remember_selected + cache_metadata: + max-age: -1 + contexts: + - 'languages:language_interface' + - url + - url.query_args + - 'user.node_grants:view' + tags: { } diff --git a/core/modules/views/tests/src/Functional/Plugin/ExposedFormTest.php b/core/modules/views/tests/src/Functional/Plugin/ExposedFormTest.php index d231907de9249658a842640cfdae2eb9fec94bcd..b653c352795cb133ae0f32573060d60f04ab1ebd 100644 --- a/core/modules/views/tests/src/Functional/Plugin/ExposedFormTest.php +++ b/core/modules/views/tests/src/Functional/Plugin/ExposedFormTest.php @@ -24,7 +24,7 @@ class ExposedFormTest extends ViewTestBase { * * @var array */ - public static $testViews = ['test_exposed_form_buttons', 'test_exposed_block', 'test_exposed_form_sort_items_per_page', 'test_exposed_form_pager']; + public static $testViews = ['test_exposed_form_buttons', 'test_exposed_block', 'test_exposed_form_sort_items_per_page', 'test_exposed_form_pager', 'test_remember_selected']; /** * Modules to enable. @@ -510,4 +510,17 @@ protected function assertNodesExist(array $bundles): void { } } + /** + * Tests the "Remember the last selection" functionality. + */ + public function testRememberSelected() { + $this->drupalGet('test_remember_selected'); + $this->getSession()->getPage()->fillField('type', 'page'); + $this->getSession()->getPage()->pressButton('Apply'); + + // Reload the page and ensure the filter is selected. + $this->drupalGet('test_remember_selected'); + $this->assertTrue($this->assertSession()->optionExists('type', 'page')->isSelected()); + } + }