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.

General Questions /

General questions about getting started with SilverStripe that don't fit in any of the categories above.

Moderators: martimiz, Sean, biapar, Willr, Ingo, swaiba, simon_w

Publishing a SiteTree object created from a GridField


Reply


4 Posts   545 Views

Avatar
purplespider

Community Member, 82 Posts

14 November 2012 at 2:21am

I've got a GridField on a NewsHolder page listing NewArticlePages. Everything works great, and I can click the Create News Article Page button to create a new article. However, when I click Create, it saves the article, but doesn't publish it.

How can make it also Publish the article when it's first created?

Avatar
BlueO

Community Member, 52 Posts

15 November 2012 at 9:40pm

Edited: 15/11/2012 9:41pm

Hey I'm not an expert so open to correction but I would try using an onAfterWrite() eg on NewsArticlePage

public function onAfterWrite() {
$this->publish();
parent::onAfterWrite();
}

as in [url]http://doc.silverstripe.org/framework/en/topics/datamodel#onbeforewrite [/url] (but with after)

Avatar
purplespider

Community Member, 82 Posts

16 November 2012 at 12:41am

Thanks BlueO, I did try that, but as publish() calls write() which calls onAfterWrite() (and onBeforeWrite()), it just got stuck in an endless loop!

Avatar
BlueO

Community Member, 52 Posts

16 November 2012 at 7:04am

hmmm OK, maybe an 'if' to check if its published so:

public function onAfterWrite() {
if($this->Status != 'Published'){
$this->publish();
}
parent::onAfterWrite();
}

sorry haven't tested but that should stop it doing another write if the page has already been published