<p>Try using <code>EVENT_AFTER_PROPAGATE</code> instead of <code>EVENT_AFTER_SAVE</code> as the <code>EVENT_AFTER_PROPAGATE</code> event fires after the full save process has completed for an element. See the <a href="https://craftcms.com/docs/4.x/extend/events.html#event-code-generator" rel="nofollow noreferrer">Events page in the Extending Craft docs</a> for more details.</p>
<p>To check if a new element is being saved, you should use this inside the event handler:</p>
<pre class="lang-php prettyprint-override"><code>if ($event-&gt;sender-&gt;firstSave) {
    // ...
}
</code></pre>

CC BY-SA 4.0
2022-10-25T13:45:57.887Z
Thanks, I tried this event, it still returns null for the volumeId and the folderId is still the temporary ID. Currently I can't find a way to relate the asset to a volume or the correct destination folder to be able to check if this asset is one which would be amended.
zizther
CC BY-SA 4.0
2022-10-25T14:06:56.257Z
Is this for when saving an asset on its own, or as part of an entry?
Steve Rowling
CC BY-SA 4.0
2022-10-25T14:28:55.803Z
My usecase would be an asset field in an element (Entry or User) as well as directly within the asset section of the CP.
zizther
CC BY-SA 4.0
2022-10-25T17:41:08.173Z
FWIW, `$asset->volumeId` still works on my end – tested both in a `EVENT_AFTER_SAVE` and `EVENT_AFTER_PROPAGATE` event handler, on the latest Craft 4 release. Something is likely wrong somewhere...
Mats Mikkel Rummelhoff
CC BY-SA 4.0
2022-10-25T21:33:28.357Z
Interesting. Will have to look into this more then
zizther
CC BY-SA 4.0
2022-10-25T22:35:29.743Z
I have had a play around, disabled all plugins and commented all other custom event code besides the renaming of the assets. The same issue is happening where. I've even added a new asset field to test that and the same output happens, no Volume ID and the folder details are `folderId: 16`, `uploaderId: 1300`, `folderPath: "user_1300/"` This is always the same, I presume this is the temp folder.
zizther
CC BY-SA 4.0
2022-10-25T12:57:36.153Z
2022-10-25T12:57:36.153Z
Steve Rowling
<p>I am looking to update some custom module code which updates the file name on upload. In Craft 3 I had this working by listening to the <code>EVENT_AFTER_SAVE</code> on an Asset. It would detect the ID of the volume it was uploaded to and then change the file name to a random string which then resave the entry to the folder it was saved in.</p>
<p>Since Craft 4 and the changes with filesystems I am no longer able to get back to the volume ID to compare, testing the folder ID does not help as the assets uses a temp folder until the element (User or Entry) has saved.</p>
<p>The <code>volumeId</code> returns <code>null</code> within Craft 4, the output shows <code>-_volumeId: null</code>, sometimes it does have a value against it under certain circumstances.</p>
<p>What is the best way to do this in Craft 4?</p>
<p>I have included the code I was using.</p>
<pre><code>const RENAME_ASSET_VOLUME_FOLDER_IDS = [2,3];

public static function renameAsset($event)
{
    $asset = $event-&gt;sender;

    $isNew = $event-&gt;isNew;

    // Only update the filename for new assets
    if ($isNew) {
        // Volume ID
        $volumeId = $asset-&gt;volumeId;

        // Test if asset volume ID is in volume folder array
        if (in_array($folderId, self::RENAME_ASSET_VOLUME_FOLDER_IDS)) {
            // Get folder
            $folder = $asset-&gt;getFolder();

            // File extension
            $extension = $asset-&gt;getExtension();

            // Generate random string
            $randomString = ToolboxHelper::randomString();

            // New file name
            $newfileName = $randomString . '.' . $extension;

            // Update file title
            $asset-&gt;title = $randomString;

            // Save asset
            Craft::$app-&gt;assets-&gt;moveAsset($asset, $folder, $newfileName);
        }
    }
}
</code></pre>

CC BY-SA 4.0
2022-11-08T09:52:53.277Z
but where to put this code?? i should create a module and paste this code into ?
Vitalie Jalbu
<assets><events><craft4><filesystem><volumes>
How to rename an asset on file upload Craft 4
# How to rename an asset on file upload Craft 4

(Asked by: username_0 on 2022-10-25)

I am looking to update some custom module code which updates the file name on upload. In Craft 3 I had this working by listening to the EVENT_AFTER_SAVE on an Asset. It would detect the ID of the volume it was uploaded to and then change the file name to a random string which then resave the entry to the folder it was saved in.
Since Craft 4 and the changes with filesystems I am no longer able to get back to the volume ID to compare, testing the folder ID does not help as the assets uses a temp folder until the element (User or Entry) has saved.
The volumeId returns null within Craft 4, the output shows -_volumeId: null, sometimes it does have a value against it under certain circumstances.
What is the best way to do this in Craft 4?
I have included the code I was using.
const RENAME_ASSET_VOLUME_FOLDER_IDS = [2,3];

public static function renameAsset($event)
{
    $asset = $event->sender;

    $isNew = $event->isNew;

    // Only update the filename for new assets
    if ($isNew) {
        // Volume ID
        $volumeId = $asset->volumeId;

        // Test if asset volume ID is in volume folder array
        if (in_array($folderId, self::RENAME_ASSET_VOLUME_FOLDER_IDS)) {
            // Get folder
            $folder = $asset->getFolder();

            // File extension
            $extension = $asset->getExtension();

            // Generate random string
            $randomString = ToolboxHelper::randomString();

            // New file name
            $newfileName = $randomString . '.' . $extension;

            // Update file title
            $asset->title = $randomString;

            // Save asset
            Craft::$app->assets->moveAsset($asset, $folder, $newfileName);
        }
    }
}

**Tags:** assets, events, craft4, filesystem, volumes

> **Comments:**
> **username_1**: but where to put this code?? i should create a module and paste this code into ?  

---

## 1 answers

### Answer by username_2 on 2022-10-25. Score: 2

Try using EVENT_AFTER_PROPAGATE instead of EVENT_AFTER_SAVE as the EVENT_AFTER_PROPAGATE event fires after the full save process has completed for an element. See the Events page in the Extending Craft docs for more details.
To check if a new element is being saved, you should use this inside the event handler:
if ($event->sender->firstSave) {
    // ...
}

> **Comments:**
> **username_0**: Thanks, I tried this event, it still returns null for the volumeId and the folderId is still the temporary ID. Currently I can't find a way to relate the asset to a volume or the correct destination folder to be able to check if this asset is one which would be amended.  
> **username_2**: Is this for when saving an asset on its own, or as part of an entry?  
> **username_0**: My usecase would be an asset field in an element (Entry or User) as well as directly within the asset section of the CP.  
> **username_3**: FWIW, `$asset->volumeId` still works on my end – tested both in a `EVENT_AFTER_SAVE` and `EVENT_AFTER_PROPAGATE` event handler, on the latest Craft 4 release. Something is likely wrong somewhere...  
> **username_0**: Interesting. Will have to look into this more then  
> **username_0**: I have had a play around, disabled all plugins and commented all other custom event code besides the renaming of the assets. The same issue is happening where. I've even added a new asset field to test that and the same output happens, no Volume ID and the folder details are `folderId: 16`, `uploaderId: 1300`, `folderPath: "user_1300/"` This is always the same, I presume this is the temp folder.  

---

