I have a boolean user profile field which is single on/off checkbox, says 'field_user_flag'. Its On value is 1 and Off value is 0. But I could not set #default_value = 1 from hook_form_alter to save "1" into its table field_data_field_user_flag and field_revision_field_user_flag.

When it is 0 and unchecked in user/*/edit, I would like to set it to 1 and checked. Thus, I did it in my hook_form_alter as below.

$form['field_user_flag']['#default_value'] = 1;
$form['field_user_flag']['#attributes'] = array('checked' => 'checked');

But, it did not work. I created the field in account settings (admin/config/people/accounts/fields/) without using Profile 2 module.

  • where the field axactly in your usera ccount or created with porfile2 module? – Yuseferi Sep 18 '12 at 6:07
  • 1
    I created the field in account settings (admin/config/people/accounts/fields/) without using profile2 module – Sithu Sep 18 '12 at 6:16

I tested it and worked for me:

$form['field_user_flag'][LANGUAGE_NONE]['#default_value'] = 1;

You can also set a default value of this field (in field settings in field managment).

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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