I want to send an email if/when an entry is saved with a lightswitch switched on after previously being off.

I have the below code in my plugin's init file, but I don't know how to

  1. check for the section uid and verify that it's the right section or

  2. check the previous value and compare it to the value being saved.

    Craft::$app->elements->on(Elements::EVENT_AFTER_SAVE_ELEMENT, function(ElementEvent $e) {
            $element = $e->element;

I'd appreciate any help, thanks.


I believe you're looking for this instead.

    static function (ModelEvent $event) {
        $entry = $event->sender;
        if ($entry->status === 'disabled'
            && $entry->propagating
            && $entry->resaving
            && ElementHelper::isDraftOrRevision($entry)) {
            return; // do nothing if disabled/draft/resaving...
        if ($entry->section->handle === 'my-handle') { // check section handle
            // call your preferred msg client: slack, email, mailgun...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.