1

I've been looking for an event that get triggered when an entry is moved from a disabled to enabled status.

Currently I'm trying to find it through the EVENT_AFTER_SAVE_ELEMENT event but I have no idea what the previous status of the entry was so I can't tell if it was just updated:

current

Event::on(
    Elements::class,
    Elements::EVENT_AFTER_SAVE_ELEMENT,
    function(Event $event)
    {
        $element = $event->element;

        // Get the status, but I don't know the previous status.
        $status = $element->status;
    }
);

I also had this pointed out to me https://docs.craftcms.com/api/v3/craft-events-setstatusevent.html but I don't know what emits this event as a parameter.

1 Answer 1

2

Well, that's awkward.

The SetStatusEvent was removed way back in the Craft 3.0 launch, but the class itself was never removed (will fix that for the upcoming 3.4 launch).

There isn't really a clean replacement for it.

One way would be to listen to the EVENT_BEFORE_SAVE_ELEMENT event, run a separate database query to get the current status from the database and compare the status to the element that was passed in to see if it changed.

3.4 will give you another option - you could listen to EVENT_AFTER_SAVE_ELEMENT, then check the dirtyAttributes array to see if the status has changed, since elements now keep track of what attributes have changed on a given request.

1
  • 1
    In Craft 4 it looks like status does not appear in the dirtyAttributes array when I toggle "enabled" on an entry, even though status has definitely changed from live to disabled. However, enabled does appear in dirtyAttributes.
    – tremby
    Commented May 7 at 21:50

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.