<p>Admin users can replace asset files without having to deal with re-uploading/re-creating a new asset using the feature/technique described in Peter's answer here: <a href="https://craftcms.stackexchange.com/questions/1550/is-it-possible-to-replace-uploaded-assets">Is it possible to replace uploaded assets?</a>. This has the advantage of preserving any values in the asset's custom fields.</p>

<p>I need to populate some of those fields dynamically though, so they correspond to the new file for the asset. So, I wrote a small plugin that listens to the <a href="https://craftcms.com/docs/plugins/events-reference#assets-onReplaceFile" rel="nofollow noreferrer">assets.onReplaceFile</a> event like so:</p>

<pre><code>craft()-&gt;on('assets.replaceFile', function(Event $event) {
  Craft::dd($event-&gt;params); // or whatever
});
</code></pre>

<p>But the code in the handler never gets executed. Is this the right event to listen to this specific user action or is there another event for it? Or am I doing something wrong here? Thanks!</p>

CC BY-SA 3.0
2016-11-10T23:54:29.330Z
Where does that code live?  In your plugin's `init()` method?
Brad Bell
CC BY-SA 3.0
2016-11-12T17:18:01.623Z
Yes, in the `init()` method of the plugin's main class.
yoannisj
CC BY-SA 3.0
2016-11-13T04:51:36.957Z
Can you describe the workflow you're using where you're expecting the event to get fired?
Brad Bell
<plugin-development><assets><control-panel><replace>
Listen to replacing files through CP's assets section
# Listen to replacing files through CP's assets section

(Asked by: username_0 on 2016-11-10)

Admin users can replace asset files without having to deal with re-uploading/re-creating a new asset using the feature/technique described in Peter's answer here: Is it possible to replace uploaded assets?. This has the advantage of preserving any values in the asset's custom fields.
I need to populate some of those fields dynamically though, so they correspond to the new file for the asset. So, I wrote a small plugin that listens to the assets.onReplaceFile event like so:
craft()->on('assets.replaceFile', function(Event $event) {
  Craft::dd($event->params); // or whatever
});

But the code in the handler never gets executed. Is this the right event to listen to this specific user action or is there another event for it? Or am I doing something wrong here? Thanks!

**Tags:** plugin-development, assets, control-panel, replace

> **Comments:**
> **username_1**: Where does that code live?  In your plugin's `init()` method?  
> **username_0**: Yes, in the `init()` method of the plugin's main class.  
> **username_1**: Can you describe the workflow you're using where you're expecting the event to get fired?  

---

## 0 answers

