Jump to:

22983 Posts in 11719 Topics by 2827 members

General Questions

SilverStripe Forums » General Questions » [RESOLVED] onBeforePublish returns an error / onBeforeWrite works

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: 2035 Views
  • lise
    Avatar
    Community Member
    47 Posts

    [RESOLVED] onBeforePublish returns an error / onBeforeWrite works Link to this post

    I am trying to have an email sent every time a page is published. I am thinking of using onBeforePublish() for that.

    To test it, I created a new page class :

    class PageEmail extends Page {

    function onBeforePublish() {
          
        parent::onBeforePublish();
                
          
          $subject = 'Page published '.$this->Title;
          
       
          $email = new Email();
          $email->setTo("myemail@yahoo.com");
          $email->setFrom( Email::getAdminEmail());
          $email->setSubject($subject);
          
        $email->send();
       
          
    }
    }

    When I try to "publish" a page of this type , I get the error message :

    Error: Uncaught Exception: Object->__call(): the method 'onbeforepublish' does not exist on 'PageEmail'
    At line 724 in /public_html/mydomain/silverstripe/sapphire/core/Object.php

    Note that if I replace "onBeforePublish" with "onBeforeWrite" in the class definition, I get the email *twice* even though the function is not in page.php.

    I also noticed that the function name in the error message is all in lower cases but I don't know if this is relevant.

    I am obviously doing something wrong. Could you please point me in the right direction? ( I run version 2.4 of SilverStripe )

    Thank you.

  • lise
    Avatar
    Community Member
    47 Posts

    Re: [RESOLVED] onBeforePublish returns an error / onBeforeWrite works Link to this post

    I don't seem to get a lot of help on this one....

    For those interested, onAfterPublish() seems to work but I still don't get why onBeforePublish() does not.

    Any idea? Tip? Example?

    Cheers.

  • Willr
    Avatar
    Forum Moderator
    5462 Posts

    Re: [RESOLVED] onBeforePublish returns an error / onBeforeWrite works Link to this post

    Note that if I replace "onBeforePublish" with "onBeforeWrite" in the class definition, I get the email *twice* even though the function is not in page.php.

    When you publish a page it requires 2 writes by the ORM.

    onBeforePublish() is different from onBeforeWrite() in that onBeforePublish is a extension (mix-in) function so parent::onBeforePublish() won't refer to anything in this case. Try remove that reference and include the parameter $object as per the function call definition.

  • lise
    Avatar
    Community Member
    47 Posts

    Re: [RESOLVED] onBeforePublish returns an error / onBeforeWrite works Link to this post

    Thank you Willr for resolving this mystery.

    The problem was with the call to parent::onBeforePublish(). I didn't call the parent method when I tried onAfterPublish and
    didn't imagine it could be the cause of the problem. Both functions have the same signature and both work in the above code ( once the call to parent::... is removed )

    Maybe I should mention that I placed the function (onBeforePublish / onAfterPublish ) in the class (not in the controller) because I vaguely remember somebody on this Forum mentioning that it would not work in the Controller (sorry I didn't bookmark it)

    Thanks again

    2035 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.