21301 Posts in 5736 Topics by 2603 members
General Questions
SilverStripe Forums » General Questions » Publishing a SiteTree object created from a GridField
General questions about getting started with SilverStripe that don't fit in any of the categories above.
Moderators: martimiz, Howard, Sean, Ryan M., biapar, Willr, Ingo, swaiba, simon_w
|
Page:
1
|
Go to End | |
| Author | Topic: | 232 Views |
-
Publishing a SiteTree object created from a GridField

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?
-
Re: Publishing a SiteTree object created from a GridField

15 November 2012 at 9:40pm Last edited: 15 November 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)
-
Re: Publishing a SiteTree object created from a GridField

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!
-
Re: Publishing a SiteTree object created from a GridField

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
| 232 Views | ||
|
Page:
1
|
Go to Top |

