How can I use something like the onAfterPublish() hook on a versioned dataobject in SilverStripe

Viewed 419

I have a simple versioned dataobject in SilverStripe. I'm trying to hook into the publication action and send out an email whenever the dataobject is published.

I don't think the onAfterPublish() method is available on dataobjects (only pages), so I'm looking to either mimic that or get enough logic working in the onAfterWrite() function.

Here's my code at the moment:

static $has_written = false; // Hack so it only fires once on write()
public function onAfterWrite()
{
    parent::onAfterWrite();
    if (!self::$has_written) {
        $stage = $this->getSourceQueryParam("Versioned.stage");
        if ($stage === 'Live') {
            $email = new Email();
            ...
            $email->send();
        }
    }
    self::$has_written = true;
}
1 Answers
Related