3

We need to send notification emails when entries of a particular type are created, updated, or enabled/disabled. The entrypoint for this happens in a module's init() method:

Event::on(Elements::class, Elements::EVENT_AFTER_SAVE_ELEMENT, function (Event $event) {
    // do stuff
}

We recently updated to Craft 3.3.0, which resulted in many many many emails being sent. I think these have something to do with queue tasks that are generated as a result of system updates. During the update, the craft_queue table contains many rows. The first row's description is "Updating entry drafts and revisions" and the many subsequent rows show a description value of "Updating element slugs and URIs".

First, is it correct to assume that some system updates trigger queued resaves of all entries?

Second, we need to prevent the notification emails from being sent unless the save event is triggered as the result of a user action. I see the sender property of ElementEvent, but it seems that that has to do with the current method and doesn't bubble up the event chain.

We've thought about disabling versions on these entries, but that feels more like a bandaid.

Is there a way to determine the originating source of an ElementEvent's save event? If so, is that the best way to gain more control over when these notification emails are sent? In general, I'm wondering if we can detect whether an element is being saved as the result of a system update/queue task vs a manual user action.

3

First, you'll want to check if what is being saved is a draft or revision, and if so, ignore it for your purposes. You can check via:

use craft\helpers\ElementHelper;


if (!ElementHelper::isDraftOrRevision($element)) {
}

Next if you want to avoid doing anything during "bulk saving events", on Craft 3.1.22 or later there is:

if (!$element->resaving) {
}

...which was added in Craft CMS 3.1.22

Some additional discussion that might be useful: Improve behaviour for automatic resaving of entries when saving an entry type

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.