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.
5 Answers
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);
}
}
-
1
-
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.– FLYJun 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.– taniusJul 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');}
– dxvargasJul 6, 2017 at 15:08
I found the page hard to find, but at /admin/config/people/accounts/fields you can add fields to users.
-
2
-
-
I already created my own fields in the Drupal table
users
. "Fields" create new fields outside the tableusers
.– AlexeyAug 9, 2011 at 13:29 -
that's how drupal works, why do you want to store the fields inside the users table?– AttiksAug 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)
– AlexeyAug 11, 2011 at 12:23
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:
Enable the Field UI module.
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
).Fill out the "Add new field" or "Add existing field" line at the bottom of the form and click "Save".
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.