<p>The best approach is probably to use the <a href="https://craftcms.com/classreference/fieldtypes/BaseElementFieldType#onAfterElementSave-detail" rel="nofollow"><code>onAfterElementSave</code></a> hook available to custom fieldtypes. Here's what that could look like (the following method should go into your custom fieldtype class, e.g. <code>myawesomeplugin/fieldtypes/MyAwesomePluginFieldType.php</code>):</p>

<pre><code>public function onAfterElementSave()
{
    $field = $this;
    $fieldId = $this-&gt;model-&gt;id;
    $fieldHandle = $this-&gt;model-&gt;handle;
    $element = $this-&gt;element; // This is the UserModel, in your case
    $fieldContent = $element-&gt;getContent()-&gt;getAttribute($fieldHandle); // This is the content for the field

    // Do something awesome with the data above
    ...

}
</code></pre>

CC BY-SA 3.0
2016-10-28T11:28:55.530Z
Thanks for the reply, and i love the onAfterElementSave().
But i need to use the business field from my CustomField Type to be saved after the user is saved in a address model.

I am only getting the UserModel and not the AddressModel to populate it.
Bram B
CC BY-SA 3.0
2016-10-28T09:36:05.863Z
2016-10-28T09:54:24.270Z
2016-10-28T09:54:24.270Z
Mats Mikkel Rummelhoff
<p>I have made a custom fieldtype.</p>

<pre><code>{% import "_includes/forms" as forms %}

{{ forms.textField({
    label: "business"|t,
    required: true,
}) }}
</code></pre>

<p>And added it to the user fields.
Where do I need to save data entered in at a user profile.</p>

<p>I would like to hook into the <code>onSaveUser</code> event with my plugin to save my values into the address model of Craft Commerce, but how would I do that?</p>

<p>An example would be appreciated.</p>

<plugin-development><plugin-craftcommerce>
Custom Fieldtype in a plugin at user profile save
# Custom Fieldtype in a plugin at user profile save

(Asked by: username_0 on 2016-10-28)

I have made a custom fieldtype.
{% import "_includes/forms" as forms %}

{{ forms.textField({
    label: "business"|t,
    required: true,
}) }}

And added it to the user fields.
Where do I need to save data entered in at a user profile.
I would like to hook into the onSaveUser event with my plugin to save my values into the address model of Craft Commerce, but how would I do that?
An example would be appreciated.

**Tags:** plugin-development, plugin-craftcommerce

---

## 1 answers

### Answer by username_1 on 2016-10-28. Score: 3

The best approach is probably to use the onAfterElementSave hook available to custom fieldtypes. Here's what that could look like (the following method should go into your custom fieldtype class, e.g. myawesomeplugin/fieldtypes/MyAwesomePluginFieldType.php):
public function onAfterElementSave()
{
    $field = $this;
    $fieldId = $this->model->id;
    $fieldHandle = $this->model->handle;
    $element = $this->element; // This is the UserModel, in your case
    $fieldContent = $element->getContent()->getAttribute($fieldHandle); // This is the content for the field

    // Do something awesome with the data above
    ...

}

> **Comments:**
> **username_0**: Thanks for the reply, and i love the onAfterElementSave(). But i need to use the business field from my CustomField Type to be saved after the user is saved in a address model.  I am only getting the UserModel and not the AddressModel to populate it.  

---

