Answers.Body: <p>I believe you're looking for this instead.</p>
  <pre><code>Event::on(
      Entry::class,
      Element::EVENT_AFTER_SAVE,
      static function (ModelEvent $event) {
          $entry = $event-&gt;sender;
          if ($entry-&gt;status === 'disabled'
              &amp;&amp; $entry-&gt;propagating
              &amp;&amp; $entry-&gt;resaving
              &amp;&amp; ElementHelper::isDraftOrRevision($entry)) {
              return; // do nothing if disabled/draft/resaving...
          }
          if ($entry-&gt;section-&gt;handle === 'my-handle') { // check section handle
              // call your preferred msg client: slack, email, mailgun...
          }
      }
  );
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2021-04-15T17:21:27.320Z
Answers.LastActivityDate: 2021-04-15T17:21:27.320Z
Answers.UserDisplayName: Leo Leoncio
Body: <p>I want to send an email if/when an entry is saved with a lightswitch switched on after previously being off.</p>
  <p>I have the below code in my plugin's init file, but I don't know how to</p>
  <ol>
  <li><p>check for the section uid and verify that it's the right section or</p>
  </li>
  <li><p>check the previous value and compare it to the value being saved.</p>
  </li>
  </ol>
  <pre><code>    Craft::$app-&gt;elements-&gt;on(Elements::EVENT_AFTER_SAVE_ELEMENT, function(ElementEvent $e) {
              $element = $e-&gt;element;
              
              
      
          });
  </code></pre>
  <p>I'd appreciate any help, thanks.</p>
  
Tags: <plugin-development><entries><events>
Title: Listening for Change at Entry Save
ThreadText: # Listening for Change at Entry Save
  
  (Asked by: username_0 on 2021-02-27)
  
  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.
  
  **Tags:** plugin-development, entries, events
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2021-04-15. Score: 1
  
  I believe you're looking for this instead.
  
  ```
  Event::on(
      Entry::class,
      Element::EVENT_AFTER_SAVE,
      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...
          }
      }
  );
  
  ```
  
  
  ---
  
  