1

craftcms/cms : 3.6

First create new entry and it store as draft

then i publish this entry

then create a new draft version of this published entry

so how to differentiate this is first draft or second draft in "ModelEvent"?

Event::on(Entry::class, Element::EVENT_BEFORE_SAVE, function(ModelEvent $e) { });

1 Answer 1

1

Could you try this:

 Event::on(
        Entry::class,
        Entry::EVENT_BEFORE_SAVE,
        function (ModelEvent $event) {
            if (ElementHelper::isDraft($event->sender)) {
                // ...
            }
        }
    );

Also you can configure it here: https://craftcms.com/docs/3.x/extend/events.html

enter image description here

Or you can try this:

use craft\events\ModelEvent;
use craft\models\EntryDraft;
use yii\base\Event;

...
Event::on(
    EntryDraft::class,
    EntryDraft::EVENT_BEFORE_SAVE,
    function (ModelEvent $event) {
        if ($event->sender->firstSave) {
            // ...
        }
    }
);
2
  • It's work for craft4, but my craft version is craftcms/cms : 3.6. So, please guide for craft3, Thank you. Commented Aug 21, 2023 at 6:10
  • @arshitkalkani What part of the code is exactly not working? Also, you can try ->isNew instead of ->firstSave instead?
    – RomanAvr
    Commented Aug 21, 2023 at 14:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.