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're retiring the forums!

The SilverStripe forums have passed their heyday. They'll stick around, but will be read only. We'd encourage you to get involved in the community via the following channels instead:

Customising the CMS /

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

onAfterPublish() Troubles

Go to End

4 Posts   1607 Views


Community Member, 245 Posts

26 May 2011 at 4:30am


I have a situation where I need to populate a unique ID field (to pass to another remote system) in a page that is formed using the url and the id of the page; the trouble is, of course, when you first create the page, it doesn't have either of these fields populated. So what I have devised to do it to use the onAfterPublish() method. I have extended SiteTreeDecorator as such:


class BlogEntryExtension extends SiteTreeDecorator {

	public function onAfterPublish(&$original) {
		if($original->isPublished()) {
			$PageObject = DataObject::get_by_id("SiteTree", $original->ID);
			if($PageObject->DisqusUniqueID=="") {
				$PageObject->DisqusUniqueID = $PageObject->RelativeLink(true) . $PageObject->ID . "/";


And have added the extension in _config.php as such:

DataObject::add_extension('BlogEntry', 'BlogEntryExtension');

What I am trying to do here is to, after publishing the page, check to see if the the DisqusUniqueID field is empty (which it will be if the page has only been published once), and if it is, then publish the page AGAIN to populate it now that we have access to the url and the id fields. But $PageObject->doPublish(); doesn't seem to be working.

Anyone have any ideas on how to achieve this? I need values from the database in order to populate this field, so how can I do it without publishing it twice? Am I right? Anyways it doesn't work. The DisqusUniqueID field doesn't populate until after I publish it MANUALLY the second time.



Community Member, 245 Posts

1 June 2011 at 8:11am

Shaking this tree again-- anyone have any experience autopopulating db fields based on the values of others?


Forum Moderator, 5523 Posts

1 June 2011 at 6:01pm

Perhaps it's not a wise idea to call doPublish() inside an onAfterPublish() as that would cause it to be published multiple times. You should use onBeforeWrite() for adding fields. Publishing will only copy fields to the *_Live table.


Community Member, 245 Posts

2 June 2011 at 6:10am

I ended up using onBeforeWrite() as in the BlogEntry object:

public function onBeforeWrite() {
       $disqus_unique_id = $this->DisqusUniqueID;
	if(!$this->DisqusUniqueID || $this->URLSegment == "new-blogentry" || strpos($disqus_unique_id, "new-blogentry")) {
	       $this->DisqusUniqueID = $this->RelativeLink(true) . $this->ID . "/";

Thanks again for your attention. I guess onBeforeWrite() was indeed the method I needed.