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 9, 2022
1 parent a57eeb6 commit a8aa456
Showing 1 changed file with 27 additions and 2 deletions.
29 changes: 27 additions & 2 deletions src/Forms/GridField/GridFieldDetailForm_ItemRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -561,7 +561,7 @@ public function getEditLink($id)
$id
);

return $this->getStateManager()->addStateToURL($this->gridField, $link);
return $this->gridField->addAllStateToUrl($link);
}

/**
Expand Down Expand Up @@ -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'];
}

/**
Expand Down

0 comments on commit a8aa456

Please sign in to comment.