1

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.

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
    ...

}
  • 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 Oct 28 '16 at 11:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.