0

I'm trying to create a dynamic rerouter based on reroute plugin, but with the improvement to automatically create a reroute when a new entry is saved. So I listen for the entries.onSaveEntry event, but how can I find its route ?

Thanks a lot !

2

You can get an entry’s URI via $entry->uri.

If you want to be able to tell if an entry’s URI is actually changing, you will also need to listen to the onBeforeSafeEntry event, and store its original URI:

private $_oldUris;

public function init()
{
    craft()->on('entries.beforeSaveEntry', function(Event $event) {
        /** @var EntryModel $entry */
        $entry = $event->params['entry'];

        // Store the entry's old URI
        if ($entry->id)
        {
            $this->_oldUris[$entry->id] = $entry->uri;
        }
    });

    craft()->on('entries.saveEntry', function(Event $event) {
        /** @var EntryModel $entry */
        $entry = $event->params['entry'];

        // Did the URI just change?
        if (isset($this->_oldUris[$entry->id]) && $this->_oldUris[$entry->id] != $entry->uri)
        {
            // Yes it did. The new URI is available at $entry->uri.
            // ...
        }
    });
}
0

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.