Answers.Body: <p>I got this working by using the EVENT_BEFORE_SAVE_ELEMENT and adding the asset to the entry asset field 'pdfThumbnailImage' by using the setFieldValue.</p>
  
  <pre><code> $entry-&gt;setFieldValue('pdfThumbnailImage', [$assetId]);
  </code></pre>
  
  <p>You dont need to save the element - leave this out</p>
  
  <pre><code> Craft::$app-&gt;elements-&gt;saveElement($element); // causes loop
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-07-04T15:38:31.053Z
Answers.Text: Thank you, but this doesn't work for the title field. Custom fields can be updated like this using `EVENT_BEFORE_SAVE_ELEMENT`, but the title gets changed again after this.
Answers.UserDisplayName: Arthur
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-07-04T15:50:22.397Z
Answers.Text: Are you using setFieldValue('title') ?
  What about something like :
  
  if(isset($fields['title'])) { 
          $entry->title = $fields['title'];     
          unset($fields['title']); 
      }
  
      if(isset($fields['slug'])) {
          $entry->slug = $fields['slug'];     
          unset($fields['slug']);
      }
  
      $entry->setFieldValues($fields);
  via -> https://craftcms.stackexchange.com/questions/20962/how-to-save-a-new-entry-with-custom-fields-craft-3
Answers.UserDisplayName: joomkit
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-07-04T16:08:32.593Z
Answers.Text: `$element->setFieldValue('title')` is an Unknown Property, and setting `$element->title` doesn't work as it gets overwritten after the event.
Answers.UserDisplayName: Arthur
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-07-04T16:44:47.217Z
Answers.Text: Just tested in my plugin : $entry->title = "Fooobar"; worked fine when saving in the CP.  That's without Craft::$app->elements->saveElement($element);  Maybe check you are not saving the element and use the event  EVENT_BEFORE_SAVE_ELEMENT. Just setting the title $entry->title = 'whatever'
Answers.UserDisplayName: joomkit
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-07-04T17:44:20.273Z
Answers.Text: Ah, it wasn't working for me because I had **Show the Title field** disabled with a temporary name in the **Title Format** field. That was overwriting my code. When I turn **Show the Title field** back on the `$entry->title = "Fooobar"` works. Thank you so much for your help. :)
Answers.UserDisplayName: Arthur
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-07-05T10:14:28.923Z
Answers.Text: Credit to Robin via slack who helped me with this too!
Answers.UserDisplayName: joomkit
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-07-03T16:56:47.987Z
Answers.LastActivityDate: 2018-07-03T16:56:47.987Z
Answers.UserDisplayName: joomkit
Body: <p>I am trying to update an entry's title in a Craft 3 module after an entry is saved. I cannot use EVENT_BEFORE_SAVE_ELEMENT because the title gets updated after this. If I use EVENT_AFTER_SAVE_ELEMENT to update the title and then do <code>saveElement</code> the event gets called again and I get into an infinite loop. </p>
  
  <pre><code>Event::on(Elements::class, Elements::EVENT_AFTER_SAVE_ELEMENT, function(Event $event) {
      if ($event-&gt;element instanceof \craft\elements\Entry) {
          if ($element-&gt;section-&gt;id == 1 &amp;&amp; $element-&gt;typeId == 1) {
              $element = $event-&gt;element;
              $newTitle = "new title"; // logic goes here
              $element-&gt;title = $newTitle;
              Craft::$app-&gt;elements-&gt;saveElement($element); // causes loop
          }
      }
  });
  </code></pre>
  
  <p>Is there a way to do this? Thanks.</p>
  
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2018-07-03T16:05:18.467Z
Comments.Text: Yes how do you do this? Seems a lack of documentation or examples for events and plugins
Comments.UserDisplayName: joomkit
Tags: <craft3><events>
Title: How to saveElement in EVENT_AFTER_SAVE_ELEMENT without infinite loop
ThreadText: # How to saveElement in EVENT_AFTER_SAVE_ELEMENT without infinite loop
  
  (Asked by: username_0 on 2018-07-02)
  
  I am trying to update an entry's title in a Craft 3 module after an entry is saved. I cannot use EVENT\_BEFORE\_SAVE\_ELEMENT because the title gets updated after this. If I use EVENT\_AFTER\_SAVE\_ELEMENT to update the title and then do `saveElement` the event gets called again and I get into an infinite loop.
  
  ```
  Event::on(Elements::class, Elements::EVENT_AFTER_SAVE_ELEMENT, function(Event $event) {
      if ($event->element instanceof \craft\elements\Entry) {
          if ($element->section->id == 1 && $element->typeId == 1) {
              $element = $event->element;
              $newTitle = "new title"; // logic goes here
              $element->title = $newTitle;
              Craft::$app->elements->saveElement($element); // causes loop
          }
      }
  });
  
  ```
  
  Is there a way to do this? Thanks.
  
  **Tags:** craft3, events
  
  > **Comments:**
  > **username_1**: Yes how do you do this? Seems a lack of documentation or examples for events and plugins  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2018-07-03. Score: 3 | Accepted answer
  
  I got this working by using the EVENT\_BEFORE\_SAVE\_ELEMENT and adding the asset to the entry asset field 'pdfThumbnailImage' by using the setFieldValue.
  
  ```
   $entry->setFieldValue('pdfThumbnailImage', [$assetId]);
  
  ```
  
  You dont need to save the element - leave this out
  
  ```
   Craft::$app->elements->saveElement($element); // causes loop
  
  ```
  
  > **Comments:**
  > **username_0**: Thank you, but this doesn't work for the title field. Custom fields can be updated like this using `EVENT_BEFORE_SAVE_ELEMENT`, but the title gets changed again after this.  
  > **username_1**: Are you using setFieldValue('title') ? What about something like :  if(isset($fields['title'])) {          $entry->title = $fields['title'];              unset($fields['title']);      }      if(isset($fields['slug'])) {         $entry->slug = $fields['slug'];              unset($fields['slug']);     }      $entry->setFieldValues($fields); via -> https://craftcms.stackexchange.com/questions/20962/how-to-save-a-new-entry-with-custom-fields-craft-3  
  > **username_0**: `$element->setFieldValue('title')` is an Unknown Property, and setting `$element->title` doesn't work as it gets overwritten after the event.  
  > **username_1**: Just tested in my plugin : $entry->title = "Fooobar"; worked fine when saving in the CP.  That's without Craft::$app->elements->saveElement($element);  Maybe check you are not saving the element and use the event  EVENT_BEFORE_SAVE_ELEMENT. Just setting the title $entry->title = 'whatever'  
  > **username_0**: Ah, it wasn't working for me because I had **Show the Title field** disabled with a temporary name in the **Title Format** field. That was overwriting my code. When I turn **Show the Title field** back on the `$entry->title = "Fooobar"` works. Thank you so much for your help. :)  
  > **username_1**: Credit to Robin via slack who helped me with this too!  
  
  ---
  
  