From a8aa45613b2c94c8f8fc16e86ae0ea45343dddbd Mon Sep 17 00:00:00 2001 From: Sabina Talipova Date: Fri, 5 Aug 2022 12:15:09 +1200 Subject: [PATCH] ENH Update page number in the state on reaching the first or the last element in a list FIX --- .../GridFieldDetailForm_ItemRequest.php | 29 +++++++++++++++++-- 1 file changed, 27 insertions(+), 2 deletions(-) diff --git a/src/Forms/GridField/GridFieldDetailForm_ItemRequest.php b/src/Forms/GridField/GridFieldDetailForm_ItemRequest.php index 1bc42369bcc..0da1d6ffa0b 100644 --- a/src/Forms/GridField/GridFieldDetailForm_ItemRequest.php +++ b/src/Forms/GridField/GridFieldDetailForm_ItemRequest.php @@ -561,7 +561,7 @@ public function getEditLink($id) $id ); - return $this->getStateManager()->addStateToURL($this->gridField, $link); + return $this->gridField->addAllStateToUrl($link); } /** @@ -591,7 +591,32 @@ private function getAdjacentRecordID($offset) $map = $list->limit($limit, $limitOffset)->column('ID'); $index = array_search($this->record->ID, $map ?? []); - return isset($map[$index+$offset]) ? $map[$index+$offset] : false; + $position = $index + $offset; + $page = $currentPage; + $lastPosition = count($map) - 1; + $hasMorePages = $this->getNumPages($gridField) > $currentPage; + + if ($position === 0 && $currentPage > 1) { + $page = $currentPage - 1; + } elseif ($position === $lastPosition && $hasMorePages) { + $page = $currentPage + 1; + } + $gridField->State->GridFieldPaginator->currentPage = (int)$page; + + return isset($map[$position]) ? $map[$position] : false; + } + + /** + * Gets the number of GridField pages + * @return int + */ + public function getNumPages(GridField $gridField) + { + return $gridField + ->getConfig() + ->getComponentByType(GridFieldPaginator::class) + ->getTemplateParameters($gridField) + ->toMap()['NumPages']; } /**