Skip to content

Commit

Permalink
ENH Update page number in the state on reaching the first or the last…
Browse files Browse the repository at this point in the history
… element in a list

FIX
  • Loading branch information
Sabina Talipova committed Aug 8, 2022
1 parent a57eeb6 commit d029a58
Showing 1 changed file with 25 additions and 1 deletion.
26 changes: 25 additions & 1 deletion src/Forms/GridField/GridFieldDetailForm_ItemRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -591,7 +591,31 @@ 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;
$hasMorePages = $this->getNumPages($gridField) > $currentPage;

if ($position === 0 && $currentPage > 1) {
$page = $currentPage - 1;
} elseif ($position === $itemsPerPage && $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'];
}

/**
Expand Down

0 comments on commit d029a58

Please sign in to comment.