aftersave revision in craft cms code example

Example: aftersave revision in craft cms

Event::on(
    \craft\services\Elements::class,
    \craft\services\Elements::EVENT_AFTER_SAVE_ELEMENT,
    function (\craft\events\ElementEvent $event) {
        $element = $event->element;
        if(
            $element instanceof \craft\elements\Entry // is entry
            && $element->getSection()->handle == 'pages' // is in 'pages' section
            && !$element->getIsRevision() // is not revision
            && !$element->getIsDraft() // is not draft
            // && !$element->propagating // not during propagating (avoid batch propagating)
            // && !$element->resaving // not during resaving (avoid batch resaving)
        ) {
            // --------------- My Stuff -------------- //
            // $entry is the $element
            // $isNew = $event->isNew
            // ...
        }
    }
);

Tags:

Misc Example