23

I'd like to add additional fields to the user profile. I'd like user to be able to edit almost all the information I store in the database about him. I have some ideas: maybe it's possible to do using form api.

1

5 Answers 5

24

A way to add user fields by code so you can put this in your module.

I have found this: field_create_field with in the comments a way to create a field for your user upon enabling your module:

/**
 * Implementation of hook_enable().
 */
function MYMODULE_enable() {
  // Check if our field is not already created.
  if (!field_info_field('field_myField')) {
    $field = array(
        'field_name' => 'field_myField', 
        'type' => 'text', 
    );
    field_create_field($field);

    // Create the instance on the bundle.
    $instance = array(
        'field_name' => 'field_myField', 
        'entity_type' => 'user', 
        'label' => 'My Field Name', 
        'bundle' => 'user', 
        // If you don't set the "required" property then the field wont be required by default.
        'required' => TRUE,
        'settings' => array(
           // Here you inform either or not you want this field showing up on the registration form.
            'user_register_form' => 1,
        ),
        'widget' => array(
            'type' => 'textfield',
            'weight' => '1',
        ), 
    );
    field_create_instance($instance);
  }
}
5
  • 1
    I found this very useful page which explains how to export fields that were created with the UI. This export can then be used with field_create_field and field_create_instance to do everything programmatically
    – Whiskey
    Aug 30, 2012 at 8:22
  • Thanks, this was very useful - have an idea how to auto assign the weight as well? Jun 10, 2013 at 13:15
  • you can add 'weight' => '1', to the widget array in the $instance I'll add it in my answer.
    – FLY
    Jun 10, 2013 at 14:23
  • For complex cases, it is useful to first add the field manually on /admin/config/people/accounts/fields, then use Field Inspector on /admin/config/development/field-inspector to export the field and field instance definition arrays for use in code like the above.
    – tanius
    Jul 15, 2016 at 11:21
  • To keep DB clean, you may also want to implement an hook_uninstall to delete the field. function MYMODULE_uninstall() {field_delete_field('field_myField');}
    – dxvargas
    Jul 6, 2017 at 15:08
38

I found the page hard to find, but at /admin/config/people/accounts/fields you can add fields to users.

9
  • 2
    Thank you, but can i do it using form api? I need more functionality.
    – Alexey
    Aug 2, 2011 at 11:08
  • what is it you want to do, that isn't possible with fields?
    – Attiks
    Aug 3, 2011 at 16:13
  • I already created my own fields in the Drupal table users. "Fields" create new fields outside the table users.
    – Alexey
    Aug 9, 2011 at 13:29
  • that's how drupal works, why do you want to store the fields inside the users table?
    – Attiks
    Aug 11, 2011 at 11:21
  • It's more conveniente way to do it in my opinion. By the way in order to add extra fields i use hook_form_alter(&$form, &$form_state, $form_id)
    – Alexey
    Aug 11, 2011 at 12:23
5

Profile in D7 is a bit weird. The profile2 module may do what you need.

4

In Drupal 7, use this process to add a new or existing field with different field types (such as Image, Tags field etc.) to the user profile:

  1. Enable the Field UI module.

  2. Go to "Admin → Configuration → People: Account settings" in the admin menu, and then to "Manage fields" (the second tab).

    (Alternatively, use the direct path in the URL: /admin/config/people/accounts/fields).

  3. Fill out the "Add new field" or "Add existing field" line at the bottom of the form and click "Save".

1

Which kind of fields do you want to add?

  • If this is a social networking site and you are adding privacy settings fields, use the Privacy Per User module.
  • If you wants to add tabs or accordion fields, use the field_group module to add new groups on the user account fields (URL: /admin/config/people/accounts/fields).
  • And if you want different fields for different roles, use the profile2 module.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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