0

I have created a custom field on the user entity. I would like to prepopulate this field with the value 0. So, when the user goes to their account page after registration the field is already filled in, which they are then free to change. Just like the username and email. The only way I can think to do this is to alter the registration form. I added this to my module, but when I created a new user, a new row did not appear in the field table. My module function:

function MYMODULE_form_user_register_alter(&$form, &$form_state, $form_id) {
  $form['account']['field_MYFIELD'][LANGUAGE_NONE][0] = array(
    '#type' => 'value',
    '#value' => 0,
  );
}

Where am I going wrong, or is there a better approach. Thanks.

2 Answers 2

1

Value field is only used for hidden purpose, it will not populate to user. Check below link

https://api.drupal.org/api/drupal/developer!topics!forms_api_reference.html/7#val

1
  • So, I need to pass my value of 0 to the default submit handler which will write it to the database. Any suggestions? Thanks.
    – dbj44
    Jan 11, 2014 at 18:34
1

I had to figure this out not too long ago. Make sure you have the Field Permissions module installed. I created a new field for user accounts from admin/config/people/accounts/fields, and with the help of the Field Permissions module, set the permissions so that the only permission allowed for field is create own value for an anonymous user, and edit own value was checked for whatever role the user will have after registration. I made sure to check Display on user registration form, of course. I then hid the field via CSS display: none. Now here's the cool part:

To fill it in, you append text to the user/register url, like so:

user/register?edit[field_YOURFIELD][und][0][value]=FIELDVALUE

FIELDVALUE would be 0 in your case. I'm not sure [und][0] applies to your field, but based on the info you've provided, you'll know what's going on :)

Then, when the user has successfully registered, they can go in to their account and edit it.

1
  • Do you happen to know the corresponding solution for Drupal 8? Aug 3, 2017 at 17:07

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.