From 55d49cbf2b731252af56b54b3a4676eebc6a3be5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tobias=20K=C3=BCndig?= Date: Thu, 8 Mar 2018 16:52:21 +0000 Subject: [PATCH] Cache the last visited page in the list widget (#3432) Stores the last visited page of list widgets in the session to restore to on next page load. Does not apply when filters / searches are applied. Credit to @tobias-kuendig --- modules/backend/widgets/Lists.php | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/modules/backend/widgets/Lists.php b/modules/backend/widgets/Lists.php index 63ee13e12..d026add52 100644 --- a/modules/backend/widgets/Lists.php +++ b/modules/backend/widgets/Lists.php @@ -263,6 +263,9 @@ class Lists extends WidgetBase if ($this->showPagination) { $this->vars['pageCurrent'] = $this->records->currentPage(); + // Store the currently visited page number in the session so the same + // data can be displayed when the user returns to this list. + $this->putSession('lastVisitedPage', $this->vars['pageCurrent']); if ($this->showPageNumbers) { $this->vars['recordTotal'] = $this->records->total(); $this->vars['pageLast'] = $this->records->lastPage(); @@ -540,8 +543,13 @@ class Lists extends WidgetBase $records = $model->getNested(); } elseif ($this->showPagination) { - $method = $this->showPageNumbers ? 'paginate' : 'simplePaginate'; - $records = $model->{$method}($this->recordsPerPage, $this->currentPageNumber); + $method = $this->showPageNumbers ? 'paginate' : 'simplePaginate'; + $currentPageNumber = $this->currentPageNumber; + if (!$currentPageNumber && empty($this->searchTerm)) { + // Restore the last visited page from the session if available. + $currentPageNumber = $this->getSession('lastVisitedPage'); + } + $records = $model->{$method}($this->recordsPerPage, $currentPageNumber); } else { $records = $model->get();