0

I'm currently working on a plugin that sends an email before the Guest Entries Plugin saves the entry. But one of the fields is an asset field and the file may be renamed when uploaded so I need to switch this up so the email being sent after the entry is saved. I tried creating an onEntrySave event in the Guest Entries plugin by doing the following.

Added to GuestEntriesService.php:

/**
     * Fires an 'onSaveEntry' event.
     *
     * @param GuestEntriesEvent $event
     *
     * @return null
     */
    public function onSaveEntry(GuestEntriesEvent $event)
    {
        $this->raiseEvent('onSaveEntry', $event);
    }

In GuestEntriesController.php I've added

$this->onSaveEntry(new GuestEntriesEvent($this, array('entry'=> $entry,'isNewEntry' => true)));

to the section of the code where the entry is saved.

if (craft()->entries->saveEntry($entry))
{
     $this->onSaveEntry(new GuestEntriesEvent($this, array('entry'=> $entry,'isNewEntry' => true)));
     $this->_returnSuccess($entry);

}

But this is where I've gone wrong as I get the error:

Craft\GuestEntriesController and its behaviors do not have a method or closure named "onSaveEntry".

How do I go about modifying the plugin to trigger an onSaveEntry event?

1 Answer 1

4

It probably would be better to just write your own plugin to do this, so that you don't have to worry about patching in your changes every time the guestEntries plugin is updated — and it could technically be just one file, although the recommended craft method would be to create a service method to handle the events.

In your plugins main class file, just create an init method that listens for the entries.saveEntry event for the section in question:

public function init()
{
    parent::init();

    craft()->on('entries.saveEntry', function(Event $event) 
    {
        $entry = $event->params['entry'];
        if ($entry->section == 'MySection') 
        {
            if ($event->params['isNewEntry']) 
            {
                craft()->myplugin->handleNewSectionEntry($entry);
                // or do something directly
            } 
            else 
            {   
                craft()->myplugin->handleUpdateSectionEntry($entry);
                // or do something directly
            }
        }
    }); // edited
}

If you need to test whether it is a 'guest' entry from the front-end vs the CP you can add:

if (!$currentUser) { ... }

Your Answer

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

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