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.

General Questions /

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

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

Publishing a SiteTree object created from a GridField


Go to End


4 Posts   1083 Views

Avatar
purplespider

Community Member, 89 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 http://doc.silverstripe.org/framework/en/topics/datamodel#onbeforewrite (but with after)

Avatar
purplespider

Community Member, 89 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