onAfterElementSave and new elements 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-&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);
        }
    } 
}


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? :)
You should be using it like this:

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);
        }
    } 
}