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.

Customising the CMS /

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

onAfterPublish() Troubles


Go to End


4 Posts   2086 Views

Avatar
Garrett

Community Member, 245 Posts

26 May 2011 at 4:30am

Hi,

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:

<?php

class BlogEntryExtension extends SiteTreeDecorator {

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

}
?> 

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.

Thanks,
Garrett

Avatar
Garrett

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?

Avatar
Willr

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.

Avatar
Garrett

Community Member, 245 Posts

2 June 2011 at 6:10am

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

public function onBeforeWrite() {
       parent::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.

//Garrett