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

Is there a hook for edit after publish?


Go to End


4 Posts   2437 Views

Avatar
otherjohn

Community Member, 125 Posts

22 June 2010 at 2:09pm

Hi,
I use onBeforeWrite to call an API function and return a field before I save to the DB the first time it is published (least I think it only fires the first time).
But is there a way to tell if its a first time or a save to a already created page?

Avatar
Willr

Forum Moderator, 5523 Posts

22 June 2010 at 6:42pm

onBeforeWrite() is called whenever you call write() on the object (new, updating etc etc).

For after publish you can use onAfterPublish().

Avatar
mark_s

Community Member, 78 Posts

22 June 2010 at 10:33pm

Hi.

There is also an onBeforePublish in 2.4.

Page (SiteTree actually) has two introspection methods that might with state-specific logic:

* isNew() returns true if the page is new (and unwritten to the database)
* isPublished() returns true if the page has been published, false if never published.

Mark

Avatar
otherjohn

Community Member, 125 Posts

23 June 2010 at 1:57am

Awesome, this appears like its just what I was looking for.

So if I wanted to do this I would write it like this?

function onBeforeWrite(){
   if($this->isNew()){
     //do something
   }elseif($this->isPublished()){
     //do something else
   }else{
     ///something
   }
}