For authenticated users, we allow exposed filters to be "remembered" when that users returns to that page. We should allow the same for other exposed options such as sort and pager.

D7 version of this feature request: #1157104: remember exposed sort.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

mikeker created an issue. See original summary.

mayurjadhav’s picture

Assigned: Unassigned » mayurjadhav
dawehner’s picture

Good idea!

mikeker’s picture

Tyapchyc’s picture

mikeker’s picture

Assigned: mayurjadhav » Unassigned
Status: Active » Needs review

Let's see what the testbot says...

mikeker’s picture

Assigned: Unassigned » mikeker
Status: Needs review » Needs work
Issue tags: +Needs tests

@Tyapchyc, thank you for your work on this! I've got a few tweaks I'd like to add from things I was working on over the weekend -- I'll post a patch shortly. Mostly a couple of UI tweaks and and adding the pager offset to the values remembered.

And we'll need some tests for this... Working on those as well.

The last submitted patch, 5: views-remember_exposed-2727751-5.patch, failed testing.

andypost’s picture

Issue tags: +VDC
mikeker’s picture

Assigned: mikeker » Unassigned
Status: Needs work » Needs review

I spent a fair bit of time this week working on this problem and think that we have a few issues to solve here:

  1. "Remembering" values should be handled at the HandlerBase plugin and not repeated across each of the three (filter, sort, and pager) plugins that implement it
  2. #1 works great for filter and sort plugins, which extend HandlerBase, but not for pager plugins, which extend PluginBase (via SqlBase).
  3. I've run into a bunch of weird errors that I believe are related to caching being done incorrectly for views with remembered options (see #2742273: Views with "remembered" filters are missing the use cache context).

Assuming the caching issue (#3) is correct, I feel this issue needs to be postponed until that is resolved. (But I could be completely off-base on this -- as I said it that issue, this intersections of Views, session variables, and cache tags is well above my pay-grade!)

Also, it would be nice to get some feedback from the Views maintainers as to whether code like \Drupal\views\Plugin\views\filter\FilterPluginBase::storeExposedInput should really be in HandlerBase with the FilterBasePlugin providing the value for isDefaulted(). And if we can have SqlBase extend HandlerBase instead of PluginBase.

Otherwise we would have to move all this "remember" code into PluginBase, which seems too general for what this entails.

Or maybe there's a completely better way to fix this that I'm not seeing?

Version: 8.2.x-dev » 8.3.x-dev

Drupal 8.2.0-beta1 was released on August 3, 2016, which means new developments and disruptive changes should now be targeted against the 8.3.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.3.x-dev » 8.4.x-dev

Drupal 8.3.0-alpha1 will be released the week of January 30, 2017, which means new developments and disruptive changes should now be targeted against the 8.4.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.4.x-dev » 8.5.x-dev

Drupal 8.4.0-alpha1 will be released the week of July 31, 2017, which means new developments and disruptive changes should now be targeted against the 8.5.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.5.x-dev » 8.6.x-dev

Drupal 8.5.0-alpha1 will be released the week of January 17, 2018, which means new developments and disruptive changes should now be targeted against the 8.6.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

gooddev’s picture

Any news on this? Patch is not working with the current Drupal core Version 8.5.3
As a workaround i write my own cookies for sorting and paging and set them in the hook_views_pre_view. Unfortunately this works not with ajax-views because we need the $_GET['page'] parameter.

This is the code:

/**
 * Implements hook_views_pre_view().
 */
function mymodule_views_pre_view(ViewExecutable $view) {
  if ($view->id() == 'myCustomViewId' && $view->current_display == 'page_1') {

    //set current sort order by expanding Views-ExposedInput
    $sortBy = isset($_COOKIE["sortBy"]) ? $_COOKIE["sortBy"] : "my_search_by_default_field";
    $sortOrder = isset($_COOKIE["sortOrder"]) ? $_COOKIE["sortOrder"] : "DESC"; //default sort order
    $currentExposedFilter = $view->getExposedInput();
    $currentExposedFilter['sort_by'] = $sortBy;
    $currentExposedFilter['sort_order'] = $sortOrder;
    $view->setExposedInput($currentExposedFilter);

    //set last active page when exposed filter was not changed
    $data = isset($_COOKIE["currentExposedFilter"]) ? (array) json_decode($_COOKIE['currentExposedFilter']) : [];
    $exposedDiff = array_diff_assoc_recursive($currentExposedFilter, $data); //custom function which checks multidimension array for differences
    $currentPage = isset($_COOKIE["pastPage"]) ? $_COOKIE['pastPage'] : 0;
    //when exposed filter is different
    if (count($exposedDiff) > 0) {
      $currentPage = 0;
      $stringCurrentExposedFilter = json_encode($currentExposedFilter);
      setcookie('currentExposedFilter', $stringCurrentExposedFilter, time() + 36000);
    }
    elseif (isset($_GET['page'])) {
      $currentPage = $_GET['page'];
    }
    setcookie('pastPage', $currentPage, time() + 36000);
    $view->setCurrentPage($currentPage);
  }
}

What I also have to mention is that D8 has some problems with multivalued exposed filters and paging. See related ticket: #2897694: Views pager links incorrectly handle complex exposed filters
There is a patch which should solve the problem, otherwise the workaround is not working.

Version: 8.6.x-dev » 8.7.x-dev

Drupal 8.6.0-alpha1 will be released the week of July 16, 2018, which means new developments and disruptive changes should now be targeted against the 8.7.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.7.x-dev » 8.8.x-dev

Drupal 8.7.0-alpha1 will be released the week of March 11, 2019, which means new developments and disruptive changes should now be targeted against the 8.8.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.8.x-dev » 8.9.x-dev

Drupal 8.8.0-alpha1 will be released the week of October 14th, 2019, which means new developments and disruptive changes should now be targeted against the 8.9.x-dev branch. (Any changes to 8.9.x will also be committed to 9.0.x in preparation for Drupal 9’s release, but some changes like significant feature additions will be deferred to 9.1.x.). For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

Version: 8.9.x-dev » 9.1.x-dev

Drupal 8.9.0-beta1 was released on March 20, 2020. 8.9.x is the final, long-term support (LTS) minor release of Drupal 8, which means new developments and disruptive changes should now be targeted against the 9.1.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

smustgrave’s picture

Hello just wondering if anyone has found a solution to this problem with a hook or patch?

Version: 9.1.x-dev » 9.2.x-dev

Drupal 9.1.0-alpha1 will be released the week of October 19, 2020, which means new developments and disruptive changes should now be targeted for the 9.2.x-dev branch. For more information see the Drupal 9 minor version schedule and the Allowed changes during the Drupal 9 release cycle.

Version: 9.2.x-dev » 9.3.x-dev

Drupal 9.2.0-alpha1 will be released the week of May 3, 2021, which means new developments and disruptive changes should now be targeted for the 9.3.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.3.x-dev » 9.4.x-dev

Drupal 9.3.0-rc1 was released on November 26, 2021, which means new developments and disruptive changes should now be targeted for the 9.4.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.4.x-dev » 9.5.x-dev

Drupal 9.4.0-alpha1 was released on May 6, 2022, which means new developments and disruptive changes should now be targeted for the 9.5.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.5.x-dev » 10.1.x-dev

Drupal 9.5.0-beta2 and Drupal 10.0.0-beta2 were released on September 29, 2022, which means new developments and disruptive changes should now be targeted for the 10.1.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

needs-review-queue-bot’s picture

Status: Needs review » Needs work
FileSize
154 bytes

The Needs Review Queue Bot tested this issue. It either no longer applies to Drupal core, or fails the Drupal core commit checks. Therefore, this issue status is now "Needs work".

Apart from a re-roll or rebase, this issue may need more work to address feedback in the issue or MR comments. To progress an issue, incorporate this feedback as part of the process of updating the issue. This helps other contributors to know what is outstanding.

Consult the Drupal Contributor Guide to find step-by-step guides for working with issues.

Version: 10.1.x-dev » 11.x-dev

Drupal core is moving towards using a “main” branch. As an interim step, a new 11.x branch has been opened, as Drupal.org infrastructure cannot currently fully support a branch named main. New developments and disruptive changes should now be targeted for the 11.x branch, which currently accepts only minor-version allowed changes. For more information, see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.