1

Pre Craft 3.2, you could do something like this if you wanted to perform an action in the event you just saved a new entry:

use craft\elements\Entry;
use craft\events\ModelEvent;

Event::on(
    Entry::class,
    Entry::EVENT_AFTER_SAVE,
    function(ModelEvent $event) {
        $entry = $event->sender;
        $isNew = $event->isNew;

        if ($isNew) {
            // do something...
        }
    }
);

However, after the update, this code doesn't work as it used to. $isNew always gets evaluated to true whether resaving a pre existing entry or editing a yet-to-be-saved entry (the autosave kicks in after a couple of seconds while you're editing).

I've tried doing something like this to check if the entry is being resaved but it still gets evaluated to true:

$isBrandNew = false;
if ($event->isNew && ! $entry->propagating && ! $entry->resaving) {
    $isBrandNew = true;
}

if ($isBrandNew) {
    // do something
}

How do I fix this so that my code only runs after a brand new entry was just created, like it did before Craft 3.2?

3

We just released Craft 3.2.5 which should fix this for you.

For more context about why this happened in the first place, see https://github.com/craftcms/cms/issues/4589.

| improve this answer | |

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.