Answers.Body: <p>You should be using it like this:</p>
  
  <pre><code>public function onAfterElementSave()
  {
      $fieldHandle = $this-&gt;model-&gt;handle;
      if ($this-&gt;element!==null) {
          $myData = $this-&gt;element-&gt;title; // just an example
          if ($this-&gt;element-&gt;$fieldHandle != $myData) {
              $this-&gt;element-&gt;getContent()-&gt;setAttribute($fieldHandle, $myData);
              craft()-&gt;elements-&gt;saveElement($this-&gt;element);
          }
      } 
  }
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-08-31T10:07:36.823Z
Answers.Text: Thanks, Bob, that solved it for me. At some point I was sure that $this->element was read-only or something, so I went for the long way around, with the ElementCriteria.
Answers.UserDisplayName: André Elvan
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2020-10-22T08:11:26.680Z
Answers.Text: What's the equivalent for `$this->model->handle` in Craft 3? I can't seem to find a way to obtain the handle.
Answers.UserDisplayName: Karl
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2020-10-22T09:01:10.927Z
Answers.Text: Just `$this->handle`
Answers.UserDisplayName: Bob Olde Hampsink
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2015-08-31T10:02:18.187Z
Answers.LastActivityDate: 2019-12-18T17:17:31.933Z
Answers.LastEditDate: 2019-12-18T17:17:31.933Z
Answers.UserDisplayName: Bob Olde Hampsink
Body: <p>I have a field type that needs to do some processing, based on the data in the element it belongs to, when an element (entry, category, user, etc) is saved, and save it to the field. I'm hooking into <code>onAfterElementSave</code> and my code (simplified) is as follow:</p>
  
  <pre><code>public function onAfterElementSave()
  {
      $fieldHandle = $this-&gt;model-&gt;handle;
      $elementId = $this-&gt;element-&gt;id;
      $elementType = $this-&gt;element-&gt;getElementType();
      $elementLocale = $this-&gt;element-&gt;locale;
  
      $criteria = craft()-&gt;elements-&gt;getCriteria($elementType);
      $criteria-&gt;id = $elementId;
      $criteria-&gt;locale = $elementLocale; 
      $element = $criteria-&gt;first();
  
      if ($element!==null) {
          $myData = $element-&gt;title; // just an example
          if ($this-&gt;element-&gt;$fieldHandle != $myData) {
              $newData = array($fieldHandle =&gt; $myData);
              $element-&gt;setContent($newData);
              craft()-&gt;elements-&gt;saveElement($element);
          }
      } 
  }
  </code></pre>
  
  <p>This works fine on existing entries, but when I create new entries, $element is null, so the field isn't updated.</p>
  
  <p>What's going on and how can I fix this? Isn't onAfterElementSave actually after element save? :) </p>
  
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2015-08-31T09:59:03.657Z
Comments.Text: Looking at the `ElementsService` it looks like the element have not yet gotten an id, so you won't get any hits searching for it with the ElementCriteria
Comments.UserDisplayName: megatrond
Tags: <plugin-development><custom-fieldtype>
Title: onAfterElementSave and new elements
ThreadText: # onAfterElementSave and new elements
  
  (Asked by: username_0 on 2015-08-30)
  
  I have a field type that needs to do some processing, based on the data in the element it belongs to, when an element (entry, category, user, etc) is saved, and save it to the field. I'm hooking into `onAfterElementSave` and my code (simplified) is as follow:
  
  ```
  public function onAfterElementSave()
  {
      $fieldHandle = $this->model->handle;
      $elementId = $this->element->id;
      $elementType = $this->element->getElementType();
      $elementLocale = $this->element->locale;
  
      $criteria = craft()->elements->getCriteria($elementType);
      $criteria->id = $elementId;
      $criteria->locale = $elementLocale; 
      $element = $criteria->first();
  
      if ($element!==null) {
          $myData = $element->title; // just an example
          if ($this->element->$fieldHandle != $myData) {
              $newData = array($fieldHandle => $myData);
              $element->setContent($newData);
              craft()->elements->saveElement($element);
          }
      } 
  }
  
  ```
  
  This works fine on existing entries, but when I create new entries, $element is null, so the field isn't updated.
  
  What's going on and how can I fix this? Isn't onAfterElementSave actually after element save? :)
  
  **Tags:** plugin-development, custom-fieldtype
  
  > **Comments:**
  > **username_1**: Looking at the `ElementsService` it looks like the element have not yet gotten an id, so you won't get any hits searching for it with the ElementCriteria  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_2 on 2015-08-31. Score: 4 | Accepted answer
  
  You should be using it like this:
  
  ```
  public function onAfterElementSave()
  {
      $fieldHandle = $this->model->handle;
      if ($this->element!==null) {
          $myData = $this->element->title; // just an example
          if ($this->element->$fieldHandle != $myData) {
              $this->element->getContent()->setAttribute($fieldHandle, $myData);
              craft()->elements->saveElement($this->element);
          }
      } 
  }
  
  ```
  
  > **Comments:**
  > **username_0**: Thanks, Bob, that solved it for me. At some point I was sure that $this->element was read-only or something, so I went for the long way around, with the ElementCriteria.  
  > **username_3**: What's the equivalent for `$this->model->handle` in Craft 3? I can't seem to find a way to obtain the handle.  
  > **username_2**: Just `$this->handle`  
  
  ---
  
  