3

I have a plugin which

  • uploads one or more files
  • creates a new entry with those files as new assets in a subfolder whose name is the new entry's slug
  • sends e-mail which includes links to the new assets.

The problem I have is that the url for the new assets is not their final url (in the subfolder), but /cpresources/tempassets/field_###/filename

The relevant bit of code:

if (craft()->entries->saveEntry($entry))
{
  // send the e-mail
  ... here when I send the e-mail, the url is wrong ...
}

What do I need to do after the saveEntry?

1 Answer 1

3

I think your best bet here would be to make use of the entries.onSaveEntry event, because at the moment $entry doesn't relate to the entry you have just saved, but the entry you wanted to save so it makes sense the entry asset paths are still pointing to the temp locations.

As far as i'm aware craft()->entries->saveEntry() just returns a bool, whether or not the save was a success, and not the entry model that was just saved.

So your controller could look like this:

if (!craft()->entries->saveEntry($entry))
{
 // Only worry if something goes wrong
}

Then in an init function in your main plugin class you could do something like:

craft()->on('entries.saveEntry', function(Event $event){

 // Is this a new entry?
 if($event->params['isNewEntry'])
 {
    // This is an EntryModel object of the entry that was just saved
    $entry = $event->params['entry'];

    // Is the entry in the right section?
    if($entry->sectionId == 1)
    {
      // Send the $entry to your email function
      craft()->yourPlugin->sendEmail($entry);
    }
 }

});

I would assume at this point the asset paths are all correct as it would have been saved to the database and all the backend work done etc

This would help you out in the future as well as it wouldn't matter where the entry was created, whether in the admin or through your plugin or anywhere else, and you could be sure the email will still get triggered :)

Failing that you could also try the assets.onSaveAsset event and see if you get better results with that?

http://buildwithcraft.com/docs/plugins/events-reference

I hope this helps in some way!

2
  • Thank you! entries.onSaveEntry surely looks like the way to go, and this clarifies a lot. A complication I have (and should have included in the question) is that I need not only this new entry but also the entry representing the form, which is where the recipients are defined. May 18, 2015 at 17:24
  • Ahh yeah I see what you mean, I just had a quick look at the docs and it does state that you can listen for events anywhere, its just more reliable to have them in the init function. So maybe you could listen for the event in your controller straight after you save the entry? Either that or if you can, store the emails on the entry model, maybe comma separated in a text field, so you can retrieve them from the entry on the other side? May 18, 2015 at 17:36

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.