Entry save event context detection #9126
-
I have a case where I'm hooking into the Documentation and methods available on the event give the ability to detect whether the event relates to a draft or a revision and by extension if neither of these, then a published element. I need to detect the difference between different types of draft save events. Specifically, when Craft auto-saves a draft when a field is edited vs. when a user explicitly clicks the "Save Draft" button. The use case is a lower privileged user making edits and I need to notify a more privileged user when there are edits to check through and potentially publish. If I just check for whether the entry is a Draft in the above event, the more privileged user would get an email every time any field is changed. That could be many emails, when all we really want is a single one when the save button was clicked. It is possible (but not very robust) to look at the request object in the save event to check some parameters / headers and make a guess at context. More generally, it would be nice to be able to get more detail on context when hooking into events. |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 5 replies
-
@rob-baker-ar You could "debounce" the events using a cache key and/or a table to track whether notifications have been queued and sent for a particular element and then send those notifications on a schedule rather than immediately. You can create multiple queues in Craft or you could just use a single CLI action on a cron job to send notifications for drafts that have been saved (but not in the past 5 minutes) |
Beta Was this translation helpful? Give feedback.
-
This is possible as of Craft 3.7 thanks to some publishing workflow improvements (#7899). Going forward, if a draft wasn’t explicitly created by the author, it will be considered a “provisional draft”. So you can test for that: if ($entry->isProvisionalDraft) {
// This is a provisional draft
} |
Beta Was this translation helpful? Give feedback.
This is possible as of Craft 3.7 thanks to some publishing workflow improvements (#7899). Going forward, if a draft wasn’t explicitly created by the author, it will be considered a “provisional draft”. So you can test for that: