1

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);
        }
    }
}
1
  • but where to put this code?? i should create a module and paste this code into ? Commented Nov 8, 2022 at 9:52

1 Answer 1

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) {
    // ...
}
6
  • 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
    Commented Oct 25, 2022 at 13:45
  • Is this for when saving an asset on its own, or as part of an entry? Commented Oct 25, 2022 at 14:06
  • 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
    Commented Oct 25, 2022 at 14:28
  • 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... Commented Oct 25, 2022 at 17:41
  • Interesting. Will have to look into this more then
    – zizther
    Commented Oct 25, 2022 at 21:33

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.