Actually you can store newsitems as dataobjects but not pages. It will be easier to manage with DataobjectManager or ModelAdmin.
First install DataObjectsManager module. Then create a NewsHolder.php page in the mysite/code folder.
class NewsHolder extends Page
{
static $has_many = array (
"NewsItems" => "NewsItem"
);
public function getCMSFields()
{
$f = parent::getCMSFields();
$f->addFieldToTab("Root.Content.NewsItems", new DataObjectManager(
$this,
'NewsItems',
'NewsItem',
array('Title' => 'Title', 'Date' =>'Date'),
'getCMSFields_forPopup'
));
return $f;
}
}
class NewsHolder_Controller extends Page_Controller {
}
Then create a Dataobject named NewsItem.php in mysite/code folder.
class NewsItem extends DataObject
{
static $db = array (
'Title' => 'Text',
'Date' => 'Date',
'Content' => 'HTMLText'
);
static $has_one = array (
'NewsHolder' => 'NewsHolder',
'NewsPhoto' => 'Image'
);
public function getCMSFields_forPopup()
{
return new FieldSet(
new TextField('Title'),
new DatePickerField('Date'),
new TextareaField('Content'),
new FileIFrameField('NewsPhoto')
);
}
}
About sorting, you can write your own function inside the NewsHolder_Controller in NewsHolder.php to get the data as you wish.
function SortedNewsItems($num=10) {
return DataObject::get("NewsItem", "", "Date DESC", "", $num);
}
Then create a NewsHolder.ss in the theme directory and you can call your sorted newsitems with this code. the number next to control name represents the number of newsitems to be listed. default is 10 as we defined in the function. For pagination you can find the necessary code on forum or silverstripe documentation.
<% control SortedNewsItems(20) %>
<h2>$Title</h2>
<h3>$Date</h3>
<% control NewsImage %><% control ResizedImage(100,50) %><img src="$URL" /><% end_control %><% end_control %>
<p>$Content.FirstParagraph</p>
<p><a href="news-item/$ID">Read More</a></p>
Now you should go and create a NewsDetailPage.php under mysite/code.
class NewsDetailPage extends Page
{
}
class NewsDetailPage_Controller extends Page_Controller {
}
function NewsDetails() {
if(!isset($_GET['start']) || !is_numeric($_GET['start']) || (int)$_GET['start'] < 1) $_GET['start'] = 0;
$SQL_start = (int)$_GET['start'];
$where = Convert::raw2SQL(Director::urlParam('ID'));
$doSet = DataObject::get(
$callerClass = "NewsItem",
$filter = "`ID` = '". $where ."'",
$sort = "",
$join = ""
);
return $doSet ? $doSet : false;
}
}
The final step is creating the NewsDetailPage.ss template for showing details of the newsitem and creating two pages under the sitetree with page-types of News Holder and News Detail Page. You need only one news detail page and its URL must be "news-item". Or you can rename it in the NewsHolder.ss file, as you wish. You can create as many NewsHolder page as you want, like categories.
<% control SortedNews %>
<h3>$Title</h3>
<h4>$Date</h4>
<p>$NewsImage
$Content</p>
<% end_control %>
I tried to explain with assuming that you are beginner to Silverstripe, sorry if it is not. But anyway I think it will be useful to some other beginners.