<p>You're probably looking for <a href="https://github.com/craftcms/cms/blob/b878807db1769284a4ab37ebadb6d958b4c26536/src/base/Element.php#L2566-L2575" rel="noreferrer"><code>Element::getDirtyAttributes()</code></a> and <a href="https://github.com/craftcms/cms/blob/b878807db1769284a4ab37ebadb6d958b4c26536/src/base/Element.php#L2725-L2737" rel="noreferrer"><code>Element::getDirtyFields()</code></a>!</p>
<p>The former is for native class properties/attributes (and is based on an ActiveRecord pattern in Yii, I believe...), and the latter is for Craft's &quot;fields,&quot; which may or may not be available, based on what type of Element you're looking at (and what <code>FieldLayout</code> it uses).</p>
<p>You're on the right track! <a href="https://github.com/craftcms/cms/blob/b878807db1769284a4ab37ebadb6d958b4c26536/src/services/Elements.php#L2460-L2461" rel="noreferrer">The Element's <code>EVENT_AFTER_SAVE</code> event will fire</a> prior to <a href="https://github.com/craftcms/cms/blob/b878807db1769284a4ab37ebadb6d958b4c26536/src/services/Elements.php#L2574-L2583" rel="noreferrer">purging the information with <code>$element-&gt;markAsClean()</code></a>.</p>
<blockquote>
<p>Note that the <code>EVENT_AFTER_SAVE</code> event emitted by your element is different than the similarly-named <code>EVENT_AFTER_SAVE_ELEMENT</code> event from the Elements service!</p>
</blockquote>

CC BY-SA 4.0
2020-10-22T18:14:50.440Z
Amazing. Thanks a ton.
Brimby
CC BY-SA 4.0
2020-10-22T03:04:35.817Z
2020-10-22T03:04:35.817Z
August Miller
<p>I'm taking my first baby steps into the php side of Craft, and my php experience in general is extremely fledgling. But I'm pretty much forced to create a module for a project I have, so here I am flailing around in a space I know virtually nothing about. So thanks for reading.</p>
<p>Basically, I need to send notification emails based on specific (very specific) user actions. Right now I have it running so that I'm catching the <code>EVENT_AFTER_SAVE</code> event in my module's <code>init()</code> function and successfully sending out emails with the Mailer. So that's good. But the issue is I need to be able to see if certain fields have changed on entries to know what email to send out.</p>
<p>Is there a way to compare the pre-saved and post-saved entry, or maybe alternatively to catch the fields that are being sent to <code>/entries/save-entry</code> to determine what the user is doing? I know it's probably likely that what I really should be doing is creating my own controller and/or action that I use on my templates in places that designate what action should be taking place, but I'm so barely holding on to comprehension of what I'm doing as-is that I'd prefer to create something caveman simple first before trying to give it better coding practices. But I'm also open to any advice at all on how to proceed.</p>

<php><events><controller><modules>
Is there a way to compare what has changed on an entry while it's being saved?
# Is there a way to compare what has changed on an entry while it's being saved?

(Asked by: username_0 on 2020-10-21)

I'm taking my first baby steps into the php side of Craft, and my php experience in general is extremely fledgling. But I'm pretty much forced to create a module for a project I have, so here I am flailing around in a space I know virtually nothing about. So thanks for reading.

Basically, I need to send notification emails based on specific (very specific) user actions. Right now I have it running so that I'm catching the `EVENT_AFTER_SAVE` event in my module's `init()` function and successfully sending out emails with the Mailer. So that's good. But the issue is I need to be able to see if certain fields have changed on entries to know what email to send out.

Is there a way to compare the pre-saved and post-saved entry, or maybe alternatively to catch the fields that are being sent to `/entries/save-entry` to determine what the user is doing? I know it's probably likely that what I really should be doing is creating my own controller and/or action that I use on my templates in places that designate what action should be taking place, but I'm so barely holding on to comprehension of what I'm doing as-is that I'd prefer to create something caveman simple first before trying to give it better coding practices. But I'm also open to any advice at all on how to proceed.

**Tags:** php, events, controller, modules


---

## 1 answers

### Answer by username_1 on 2020-10-22. Score: 5 | Accepted answer

You're probably looking for [`Element::getDirtyAttributes()`](https://github.com/craftcms/cms/blob/b878807db1769284a4ab37ebadb6d958b4c26536/src/base/Element.php#L2566-L2575) and [`Element::getDirtyFields()`](https://github.com/craftcms/cms/blob/b878807db1769284a4ab37ebadb6d958b4c26536/src/base/Element.php#L2725-L2737)!

The former is for native class properties/attributes (and is based on an ActiveRecord pattern in Yii, I believe...), and the latter is for Craft's "fields," which may or may not be available, based on what type of Element you're looking at (and what `FieldLayout` it uses).

You're on the right track! [The Element's `EVENT_AFTER_SAVE` event will fire](https://github.com/craftcms/cms/blob/b878807db1769284a4ab37ebadb6d958b4c26536/src/services/Elements.php#L2460-L2461) prior to [purging the information with `$element->markAsClean()`](https://github.com/craftcms/cms/blob/b878807db1769284a4ab37ebadb6d958b4c26536/src/services/Elements.php#L2574-L2583).

> Note that the `EVENT_AFTER_SAVE` event emitted by your element is different than the similarly-named `EVENT_AFTER_SAVE_ELEMENT` event from the Elements service!

> **Comments:**
> **username_0**: Amazing. Thanks a ton.  

---

