4

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.

5

You're probably looking for Element::getDirtyAttributes() and Element::getDirtyFields()!

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 prior to purging the information with $element->markAsClean().

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!

1
  • Amazing. Thanks a ton. – Brimby Oct 22 '20 at 18:14

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.