Skip to content

Instantly share code, notes, and snippets.

@benjamin-smith
Last active March 19, 2018 15:59
Show Gist options
  • Save benjamin-smith/8165801 to your computer and use it in GitHub Desktop.
Save benjamin-smith/8165801 to your computer and use it in GitHub Desktop.
Custom Pagination in SilverStripe CMS
<?php
class CustomObject extends DataObject {
private static $db = array(
'Sort' => 'Int',
);
private static $default_sort = 'Sort ASC';
public function PrevNextPage($mode = 'next', $loop_around = TRUE)
{
if ($mode==='next') {
$sort = $this->Sort + 1;
$dir = 'ASC';
} else {
$sort = $this->Sort - 1;
$dir = 'DESC';
}
$page = self::get()->filter(array(
'ParentID' => $this->ParentID,
'Sort' => $sort
))->First();
if( ! $page && $loop_around )
{
$page = self::get()->filter(array(
'ParentID' => $this->ParentID,
))->sort('Sort', $dir)->First();
}
return $page;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment