<p>I think your best bet here would be to make use of the <code>entries.onSaveEntry</code> event, because at the moment <code>$entry</code> 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.</p>

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

<p>So your controller could look like this:</p>

<pre><code>if (!craft()-&gt;entries-&gt;saveEntry($entry))
{
 // Only worry if something goes wrong
}
</code></pre>

<p>Then in an <code>init</code> function in your main plugin class you could do something like:</p>

<pre><code>craft()-&gt;on('entries.saveEntry', function(Event $event){

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

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

});
</code></pre>

<p>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</p>

<p>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 :)</p>

<p>Failing that you could also try the <code>assets.onSaveAsset</code> event and see if you get better results with that?</p>

<p><a href="http://buildwithcraft.com/docs/plugins/events-reference" rel="nofollow">http://buildwithcraft.com/docs/plugins/events-reference</a></p>

<p>I hope this helps in some way!</p>

CC BY-SA 3.0
2015-05-18T17:24:38.090Z
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.
Marion Newlevant
CC BY-SA 3.0
2015-05-18T17:36:32.247Z
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?
Alec Ritson
CC BY-SA 3.0
2015-05-18T07:21:32.460Z
2015-05-18T07:33:01.703Z
2015-05-18T07:33:01.703Z
Alec Ritson
<p>I have a plugin which</p>

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

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

<p>The relevant bit of code:</p>

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

<p>What do I need to do after the <code>saveEntry</code>?</p>

<plugin-development><assets>
Plugin uploads an asset, need the final url
# Plugin uploads an asset, need the final url

(Asked by: username_0 on 2015-05-16)

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?

**Tags:** plugin-development, assets

---

## 1 answers

### Answer by username_1 on 2015-05-18. Score: 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!

> **Comments:**
> **username_0**: 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.  
> **username_1**: 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?  

---

