How can I rename an asset file on upload when an entry is saved (Craft 3)? I have an entry type that includes an Assets field. When this entry is created/saved via the CP or a front-end form, I want to rename the Asset file as follows:


Entry's author id. E.g: 148
01, 02, 03 based on how many entries the author has already submitted.


So files would look like: 148-01.jpg 148-02.jpg, 148-03.jpg.

At some point, I would also like to add the value of a custom field.

Also, I would like to limit this to entry for a certain type in a certain section: Section name: "Competitions", Entry type: "Submission".

Screenshot: https://cl.ly/a526f40c6d58

So far In my Craft 3 Module I have this. I can get the Asset id, but I can't get the Entry's Author Id:

Event::on(
    Elements::class,
    Elements::EVENT_AFTER_SAVE_ELEMENT,
    function (ElementEvent $event) {
        $asset = $event-&gt;element;
        $id = $event-&gt;element-&gt;getId();
        if ($event-&gt;isNew &amp;&amp; $asset instanceof craft\elements\Asset) {
            $newFileName = "new-file-name-" . $id . ".jpg";
            Craft::$app-&gt;assets-&gt;moveAsset($asset, $asset-&gt;getFolder(), $newFileName);
        }
    }
);


I found this answer https://craftcms.stackexchange.com/a/13199/298, but it's for Craft 2:

craft()-&gt;on('entries.beforeSaveEntry', function(Event $event)
{
    $entry = $event-&gt;params['entry'];

    if (($entry-&gt;section['handle'] == 'friends') &amp;&amp; ($entry-&gt;type['handle'] == 'friends'))
    {
        $title = $entry-&gt;slug;
        $assets = $entry-&gt;imgdir;

        if (count($assets) != 1)
        {
            $event-&gt;params['entry']-&gt;addError('imgdir', Craft::t('Exactly 1 asset has to be selected.'));
            $event-&gt;performAction = false;
        }
        else
        {
            $filename = $title.'.'.$assets[0]-&gt;getExtension();

            craft()-&gt;assets-&gt;renameFile($assets[0], $filename);
            craft()-&gt;assets-&gt;storeFile($assets[0]);
        }
    }
});


How can I do this in Craft 3?
The code below is working for me so far. The answer at https://craftcms.stackexchange.com/a/28168/298 helped me figure things out for Craft 3.

Event::on(
    Entry::class,
    Entry::EVENT_AFTER_SAVE,
    function(ModelEvent $event) {
        /** @var Entry $entry */
        $entry = $event-&gt;sender;
        //Set entry's author ID
        $authorId = $entry-&gt;authorId;
        //Set value of custom field
        $imageOrder = $entry-&gt;myCustomField;

        if ($entry-&gt;enabled &amp;&amp; $entry-&gt;section-&gt;handle === 'mySectionHandle' &amp;&amp; $entry-&gt;type-&gt;handle === 'myEntryTypeHandle') {
            /** @var Asset $asset */
            foreach ($entry-&gt;myAssetField as $asset) {
                $newFileName = "0" . $imageOrder . "-" . $authorId . "-" . date('YmdHis') . "." . $asset-&gt;getExtension();
                Craft::$app-&gt;assets-&gt;moveAsset($asset, $asset-&gt;getFolder(), $newFileName);
            }
        }
    }
);


Can it be improved? For e.g., can I do without the foreach loop? Let me know and I'll update this answer and then mark it as accepted.