How can I rename an asset file on upload when an entry is saved (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.