Answers.Body: <p>The code below is working for me so far. The answer at <a href="https://craftcms.stackexchange.com/a/28168/298">https://craftcms.stackexchange.com/a/28168/298</a> helped me figure things out for Craft 3.</p>
  
  <pre><code>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);
              }
          }
      }
  );
  </code></pre>
  
  <p>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.</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-10-24T13:18:32.673Z
Answers.LastActivityDate: 2018-10-24T13:18:32.673Z
Answers.UserDisplayName: Andrea DeMers
Body: <p>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:</p>
  
  <ul>
  <li><strong>Entry's author id</strong>. E.g: 148</li>
  <li>01, 02, 03 based on how many entries the author has already submitted.</li>
  </ul>
  
  <p>So files would look like: 148-01.jpg 148-02.jpg, 148-03.jpg.</p>
  
  <p>At some point, I would also like to add the value of a custom field.</p>
  
  <p>Also, I would like to limit this to entry for a certain type in a certain section: Section name: "Competitions", Entry type: "Submission".</p>
  
  <p>Screenshot: <a href="https://cl.ly/a526f40c6d58" rel="nofollow noreferrer">https://cl.ly/a526f40c6d58</a></p>
  
  <p>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:</p>
  
  <pre><code>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);
          }
      }
  );
  </code></pre>
  
  <p>I found this answer <a href="https://craftcms.stackexchange.com/a/13199/298">https://craftcms.stackexchange.com/a/13199/298</a>, but it's for Craft 2:</p>
  
  <pre><code>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]);
          }
      }
  });
  </code></pre>
  
  <p>How can I do this in Craft 3?</p>
  
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2018-10-22T14:51:46.033Z
Comments.Text: You know Assets don't have an author ID? The question is: should it be the "author"/uploader of the asset or the author of the entry or the current user?
Comments.UserDisplayName: Robin Schambach
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2018-10-22T15:32:10.870Z
Comments.Text: The author of the entry that includes the Assets field. "Entry's author id. E.g: 148"
Comments.UserDisplayName: Andrea DeMers
Tags: <assets><craft3><modules>
Title: How can I rename an asset file on upload when an entry is saved (Craft 3)?
ThreadText: # How can I rename an asset file on upload when an entry is saved (Craft 3)?
  
  (Asked by: username_0 on 2018-10-22)
  
  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->element;
          $id = $event->element->getId();
          if ($event->isNew && $asset instanceof craft\elements\Asset) {
              $newFileName = "new-file-name-" . $id . ".jpg";
              Craft::$app->assets->moveAsset($asset, $asset->getFolder(), $newFileName);
          }
      }
  );
  
  ```
  
  I found this answer <https://craftcms.stackexchange.com/a/13199/298>, but it's for Craft 2:
  
  ```
  craft()->on('entries.beforeSaveEntry', function(Event $event)
  {
      $entry = $event->params['entry'];
  
      if (($entry->section['handle'] == 'friends') && ($entry->type['handle'] == 'friends'))
      {
          $title = $entry->slug;
          $assets = $entry->imgdir;
  
          if (count($assets) != 1)
          {
              $event->params['entry']->addError('imgdir', Craft::t('Exactly 1 asset has to be selected.'));
              $event->performAction = false;
          }
          else
          {
              $filename = $title.'.'.$assets[0]->getExtension();
  
              craft()->assets->renameFile($assets[0], $filename);
              craft()->assets->storeFile($assets[0]);
          }
      }
  });
  
  ```
  
  How can I do this in Craft 3?
  
  **Tags:** assets, craft3, modules
  
  > **Comments:**
  > **username_1**: You know Assets don't have an author ID? The question is: should it be the "author"/uploader of the asset or the author of the entry or the current user?  
  > **username_0**: The author of the entry that includes the Assets field. "Entry's author id. E.g: 148"  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_0 on 2018-10-24. Score: 1 | Accepted answer
  
  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->sender;
          //Set entry's author ID
          $authorId = $entry->authorId;
          //Set value of custom field
          $imageOrder = $entry->myCustomField;
  
          if ($entry->enabled && $entry->section->handle === 'mySectionHandle' && $entry->type->handle === 'myEntryTypeHandle') {
              /** @var Asset $asset */
              foreach ($entry->myAssetField as $asset) {
                  $newFileName = "0" . $imageOrder . "-" . $authorId . "-" . date('YmdHis') . "." . $asset->getExtension();
                  Craft::$app->assets->moveAsset($asset, $asset->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.
  
  
  ---
  
  