If an model/element gets changed then I can know about it by listening to an event like onBeforeSaveEntry. This will provide me with the latest version of the model/event, but doesn't give me the changes. Something like this would be great.

craft()->on('elements.onSaveElement', function (Event $event) {
    $element = $event->params['element'];

    // $changes = ['uri' => ['old' => 'index', 'new' => 'index2']]
    $changes = $element->getUnsavedChanges(); 

I generate a routes.js that the client uses to generate menu and other stuff. I would like to listen to uri/slug changes so I can clear my (varnish) cache to update internal links. I'd like to prevent clearing my whole site if just one tiny thing gets changed. Any idea's how I can get the model changes or work around this issue?

1 Answer 1


You have to listen to the onBeforeSaveEntry event, then query the database for the existing entry (if one exists).

craft()->on('entries.onBeforeSaveEntry', function(Event $e)
        $updatedEntry = $e->params['entry'];
        $currentEntry = craft()->entries->getEntryById($updatedEntry->id);

        if($updatedEntry->getUrl() != $currentEntry->getUrl())
            // Do your thing

It might also be worth checking if the updated entry even has a URL, so you could skip that DB query if you don't need it.

You have to be careful with this event though - there's always the possibility the transaction could fail and the updated entry won't get saved to the DB. In that case, you might want to also listen to the onSaveEntry event (if you're doing anything other than writing to the DB - this would get rolled back automatically if something fails) as a way of checking if everything went okay.

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.