Jump to:

23473 Posts in 18941 Topics by 2878 members

General Questions

SilverStripe Forums » General Questions » Is there a hook for edit after publish?

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

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

Page: 1
Go to End
Author Topic: 1276 Views
  • otherjohn
    Avatar
    Community Member
    125 Posts

    Is there a hook for edit after publish? Link to this post

    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?

  • Willr
    Avatar
    Forum Moderator
    5497 Posts

    Re: Is there a hook for edit after publish? Link to this post

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

    For after publish you can use onAfterPublish().

  • mark_s
    Avatar
    Community Member
    78 Posts

    Re: Is there a hook for edit after publish? Link to this post

    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

  • otherjohn
    Avatar
    Community Member
    125 Posts

    Re: Is there a hook for edit after publish? Link to this post

    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
    }
    }

    1276 Views
Page: 1
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.