Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

We've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

DataObjectManager Module /

Discuss the DataObjectManager module, and the related ImageGallery module.

Moderators: martimiz, UncleCheese, Sean, Ed, biapar, Willr, Ingo, swaiba

DataObjects as Pages does not work :<


Go to End


3 Posts   1557 Views

Avatar
phokki

Community Member, 14 Posts

22 May 2012 at 1:31am

Hello SilverStripe folks,

I've got a newsarticle.php page and a newsarticleholder.php.

This is my NewsArticle.php

<?php
class NewsArticle extends DataObjectAsPage {

//The class of the page which will list this DataObject
static $listing_class = 'NewsArticleHolder';
//Class Naming (optional but reccomended)
static $plural_name = 'NewsArticles';
static $singular_name = 'NewsArticle';

static $db = array (
'Title' => 'Varchar(255)',
'Date' => 'Date',
'Author' => 'Text',
'Content' => 'HTMLText',
);

static $has_one = array (
'NewsHolder' => 'NewsArticleHolder',
);

//Summar fields
static $summary_fields = array(
'Title',
'Date',
'Author',
'Content'
);

function fieldLabels($includerelations = true) {
$labels = parent::fieldLabels($includerelations);

$labels['Title'] = _t('newsarticle.TITLE', 'Titel');
$labels['Date'] = _t('newsarticle.DATE', 'Datum');
$labels['Author'] = _t('newsarticle.AUTHOR', 'Auteur');
$labels['Content'] = _t('newsarticle.CONTENT', 'Niewsbericht');
return $labels;
}

function getCMSFields() {

$fields = new Fieldset();
$fields->push(new TextField('Title','Titel'), 'Content');
$fields->push($dateField = new DateField('Date','Datum'), 'Content');
$dateField->setConfig('showcalendar', true);
$dateField->setConfig('dateformat', 'dd/MM/YYYY');

$fields->push(new TextField('Author','Auteur'), 'Content');

$fields->push(new HtmlEditorField("Content", _t('SiteTree.FOO', "Content", PR_MEDIUM, 'HTML editor title')));

return $fields;
}

}

and this is my NewsArticleHolder.php

<?php

class NewsArticleHolder extends DataObjectAsPageHolder {

static $has_many = array (
'NewsArticles' => 'NewsArticle',
);

public function getCMSFields() {
$fields = parent::getCMSFields();
$fields->addFieldsToTab("Root.Content.Nieuws", new DataObjectManager($this, 'NewsArticles', 'NewsArticle'));

return $fields;
}
}

class NewsArticleHolder_Controller extends DataObjectAsPageHolder_Controller {
//This needs to know be the Class of the DataObject you want this page to list
static $item_class = 'NewsArticle';
//Set the sort for the items (defaults to Created DESC)
static $item_sort = 'Title ASC';

}
?>

So my CMS just works fine, can add newsitems but when im on the newspage,
it wont show my newsitems :(

This is my NewsArticleHolder.ss

<div class="typography">
<% if Level(2) %>
<div class="breadcrumbs">
$Breadcrumbs
</div>
<% end_if %>
<h2>$Title</h2>
<div>
$Content
</div>
<div class="articles">
<ul id="NewsList">
<% control NewsArticles %>
foo
$Title
<% end_control %>
</ul>
</div>
$Form
$PageComments
</div>

Please help me guys!! I have no idea what i'm doing wrong, and im quite new to this....

Thanks!!

Avatar
Tim Snadden

Community Member, 32 Posts

22 May 2012 at 8:08am

If you are using the Data Objects as Pages module I believe the Controller method you need in the template is 'Items' for the listing page and 'Item' for the individual news article.

Avatar
Tim Snadden

Community Member, 32 Posts

22 May 2012 at 8:11am

This article by the author of the module gives pretty good instructions on how to use it. http://www.ssbits.com/tutorials/2012/dataobject-as-pages-the-module/