Created
December 1, 2017 04:38
-
-
Save benald/083a96b757dcb737d69491a1fed01e11 to your computer and use it in GitHub Desktop.
SilverStripe ArticlesPage.php for use with Article.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class ArticlesPage extends Page { | |
private static $has_many = array ( | |
'Articles' => 'Article' | |
); | |
public function getCMSFields() { | |
$fields = parent::getCMSFields(); | |
$fields->removeByName("Banner"); | |
$fields->removeByName("Documents"); | |
$fields->removeByName("Teaser"); | |
$fields->addFieldToTab('Root.Articles', GridField::create( | |
'Articles', | |
'Articles on this page', | |
$this->Articles(), | |
GridFieldConfig_RecordEditor::create() | |
)); | |
return $fields; | |
} | |
} | |
class ArticlesPage_Controller extends Page_Controller { | |
private static $allowed_actions = array ( | |
'article' | |
); | |
public function article(SS_HTTPRequest $request) { | |
$article = Article::get()->byID($request->param('ID')); | |
if(!$article) { | |
return $this->httpError(404, 'That article could not be found'); | |
} | |
return array ( | |
'Article' => $article, | |
'Title' => $article->Title | |
); | |
} | |
protected $articleList; | |
public function init () { | |
parent::init(); | |
$this->articleList = Article::get()->sort('Date DESC'); | |
} | |
// Pagination | |
public function PaginatedArticles ($num = 5) { | |
return PaginatedList::create( | |
$this->articleList, | |
$this->getRequest() | |
)->setPageLength($num); | |
} | |
// Lists | |
public function AllArticles() { | |
return Article::get() | |
->sort('Created', 'DESC'); | |
} | |
public function LatestArticles() { | |
return Article::get() | |
->sort('Created', 'ASC'); | |
} | |
public function Types() { | |
return Type::get() | |
->sort('Created', 'ASC'); | |
} | |
public function Industries() { | |
return Industry::get() | |
->sort('Created', 'ASC'); | |
} | |
public function Approaches() { | |
return Approach::get() | |
->sort('Created', 'ASC'); | |
} | |
public function Services() { | |
return Service::get() | |
->sort('Created', 'ASC'); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment