Answers.Body: <p>You can use <a href="https://docs.craftcms.com/api/v3/craft-base-element.html#method-getdirtyfields" rel="nofollow noreferrer">Element::getDirtyFields()</a> and <a href="https://docs.craftcms.com/api/v3/craft-base-element.html#method-getdirtyattributes" rel="nofollow noreferrer">Element::getDirtyAttributes()</a> to find which custom fields and which attributes (like authorId, postDate, etc) are marked as dirty (modified).</p>
  <pre class="lang-php prettyprint-override"><code>Event::on(
      Entry::class,
      Element::EVENT_BEFORE_SAVE,
      function (ModelEvent $modelEvent) {
          $entry = $modelEvent-&gt;sender;
          $dirtyFields = $entry-&gt;getDirtyFields();
          $dirtyAttributes = $entry-&gt;getDirtyAttributes();
      }
  );
  </code></pre>
  <p>This will only tell you which fields/attributes were marked as dirty. The element itself will only give you the current values of the element (even before saving). To compare them with the current saved state, the easiest solution would be to use an entry query to get it directly from the database.</p>
  <p>Keep in mind that a field can be marked as dirty from everywhere, so there may be situations where a field is marked as dirty but the value hasn't actually changed. So depending on what you're doing with that, you may want to check if the values are actually different.</p>
  <p>Depending on the context, <code>Element::getCanonical()</code> may also give you the previous state. But this is mostly used to compare drafts and revisions with their canonical version, so the differences may <em>not</em>  match the list of dirty fields/attributes. Whatever approach you end up using, make sure to test this in multiple scenarios to cover all edge cases.</p>
  <hr />
  <p>For different scenarios there are also the following methods:</p>
  <ul>
  <li><code>Element::getModifiedAttributes()</code> and <code>Element::getModifiedFields()</code> to get changed between an element and it's canonical (published) version. Mostly useful for comparing drafts and revisions.</li>
  <li><code>Element::getOutdatedAttributes()</code> and <code>Element::getOutdatedFields()</code> to find changes between the current Element and the last saved version. Slightly different from the two methods above, I'm not quite sure in which scenarios those are used respectively.</li>
  </ul>
  <p>Note that the methods above are only available in Craft 3.7+ and they're not listed in the API reference yet. But you can find them in the <code>ElementInterface</code> and <code>Element</code> classes in the source code for 3.7.</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2021-11-27T08:43:18.497Z
Answers.Text: Thank you very much!
Answers.UserDisplayName: Dion Snoeijen
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2021-11-26T16:05:04.063Z
Answers.LastActivityDate: 2021-11-26T16:05:04.063Z
Answers.UserDisplayName: MoritzLost
Body: <p>I want to see what data is changing whenever someone is updating an element to act on the difference between the states. In this particular case a User, but more abstractly speaking, an element.</p>
  <p>In essence I would like to be able to do something along the lines of this:</p>
  <pre class="lang-php prettyprint-override"><code>Event::on(Entry::class, Element::EVENT_BEFORE_SAVE, function (ModelEvent $modelEvent) {
  
      $entry = $modelEvent-&gt;sender;
      $beforeSave = $entry-&gt;fieldsBeforeSave();
      $newFieldData = $entry-&gt;&quot;just the current state of the Element&quot;
  
  });
  </code></pre>
  <p>Psuedo above, hopefully to clarify what I need ;).</p>
  <p>Hopefully this is possible, I haven't been able to find an answer.</p>
  
Tags: <plugin-development><modules>
Title: How can I see what fields changed on EVENT_BOEFORE_SAVE and or EVENT_AFTER_SAVE
ThreadText: # How can I see what fields changed on EVENT_BOEFORE_SAVE and or EVENT_AFTER_SAVE
  
  (Asked by: username_0 on 2021-11-26)
  
  I want to see what data is changing whenever someone is updating an element to act on the difference between the states. In this particular case a User, but more abstractly speaking, an element.
  
  In essence I would like to be able to do something along the lines of this:
  
  ```php
  Event::on(Entry::class, Element::EVENT_BEFORE_SAVE, function (ModelEvent $modelEvent) {
  
      $entry = $modelEvent->sender;
      $beforeSave = $entry->fieldsBeforeSave();
      $newFieldData = $entry->"just the current state of the Element"
  
  });
  
  ```
  
  Psuedo above, hopefully to clarify what I need ;).
  
  Hopefully this is possible, I haven't been able to find an answer.
  
  **Tags:** plugin-development, modules
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2021-11-26. Score: 2 | Accepted answer
  
  You can use [Element::getDirtyFields()](https://docs.craftcms.com/api/v3/craft-base-element.html#method-getdirtyfields) and [Element::getDirtyAttributes()](https://docs.craftcms.com/api/v3/craft-base-element.html#method-getdirtyattributes) to find which custom fields and which attributes (like authorId, postDate, etc) are marked as dirty (modified).
  
  ```php
  Event::on(
      Entry::class,
      Element::EVENT_BEFORE_SAVE,
      function (ModelEvent $modelEvent) {
          $entry = $modelEvent->sender;
          $dirtyFields = $entry->getDirtyFields();
          $dirtyAttributes = $entry->getDirtyAttributes();
      }
  );
  
  ```
  
  This will only tell you which fields/attributes were marked as dirty. The element itself will only give you the current values of the element (even before saving). To compare them with the current saved state, the easiest solution would be to use an entry query to get it directly from the database.
  
  Keep in mind that a field can be marked as dirty from everywhere, so there may be situations where a field is marked as dirty but the value hasn't actually changed. So depending on what you're doing with that, you may want to check if the values are actually different.
  
  Depending on the context, `Element::getCanonical()` may also give you the previous state. But this is mostly used to compare drafts and revisions with their canonical version, so the differences may *not* match the list of dirty fields/attributes. Whatever approach you end up using, make sure to test this in multiple scenarios to cover all edge cases.
  
  ---
  
  For different scenarios there are also the following methods:
  
  * `Element::getModifiedAttributes()` and `Element::getModifiedFields()` to get changed between an element and it's canonical (published) version. Mostly useful for comparing drafts and revisions.
  * `Element::getOutdatedAttributes()` and `Element::getOutdatedFields()` to find changes between the current Element and the last saved version. Slightly different from the two methods above, I'm not quite sure in which scenarios those are used respectively.
  
  Note that the methods above are only available in Craft 3.7+ and they're not listed in the API reference yet. But you can find them in the `ElementInterface` and `Element` classes in the source code for 3.7.
  
  > **Comments:**
  > **username_0**: Thank you very much!  
  
  ---
  
  