1

I am developing a Drupal module that will create a new user account as part of its functionality.

However, I would like to be able to include some fields that will be saved to the new user's profile when the user is created. This is easy enough for text fields, but I don't want to replicate the logic that's already been built for more complex fields.

Is there a way I can incorporate profile fields into my form using Fields API?

Thanks,

James

apaderno
  • 28,547
  • 16
  • 75
  • 90
James Shields
  • 309
  • 4
  • 12

1 Answers1

1

You can use the field_attach_form() and field_attach_submit() functions for this:

function MYMODULE_form($form, &$form_state, $account) {
  $form['#account'] = $account;
  field_attach_form('user', $account, $form, $form_state);
}

function MYMODULE_form_submit($form, &$form_state) {
  field_attach_submit('user', $form['#account'], $form, $form_state);
}
Clive
  • 36,918
  • 8
  • 87
  • 113
  • That looks like it ought to do what I want... thanks! I'll give it a try and let you know. – James Shields Jun 16 '12 at 17:13
  • This solution is working perfectly for me so far. I've even managed to put multiple instances of the fields on the form by setting the #parents property. – James Shields Jul 27 '12 at 07:29
  • Thanks again, Clive, for getting me started on this. The entity/field attachment APIs take a bit of getting used to, and some changes to the form design approach, but once you get the hang of them, they are extremely powerful. – James Shields Aug 03 '12 at 07:04