drupaldrupal-7drupal-fapidrupal-field-api

Drupal 7: Incorporate Profile fields into form


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


Solution

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