0

I am creating a user programmatically and there is a boolean field (single on/off checkbox) being used as a terms of use inside the users profile.

How do I set the value of it when creating a user object? I need it to be checked or 1.

$new_user = array(
                'name' => $username,
                'mail' => $email,
                //'pass' => $pass,
                'signature_format' => 'full_html',
                'status' => 1,
                'access' => 0,
                'timezone' => 'America/New_York',
                'init' => $email,
                'field_user_account_terms' => array(
                    'attributes' => array(
                        'checked' => 'checked',
                    ),
                ),
              );

$account = user_save(NULL, $new_user);

i've also tried

                    'field_user_account_terms' => array(
                    'und' => array(
                        0 => array(
                            'value' =>  1,
                        ),
                    ),
                ),          

I've also tried setting the default value and a bunch of other syntax. Nothing seems to be working for me.

is this even possible? What is the correct syntax for setting the value of a checkbox, to be saved in a users profile?

2
  • Are you sure "1" is the checkbox key? You'll need to use the field value key there, as per your second solution above. Commented Jan 12, 2016 at 2:02
  • @DavidThomas i'm not sure I just assumed 0 or 1 because it's boolean. or checked because it's a checkbox. I can't find a working solution.
    – DMAC
    Commented Jan 12, 2016 at 15:55

1 Answer 1

0

The help message for this field says: If left empty, "1" will be used and if left empty, "0" will be used. So I left this field blank and assumed you could programmatically set the value while creating a user. This doesn't work at all for me.

enter image description here

You actually have to explicitly define the On and Off values in order to be able to set them while creating a user.

enter image description here

Then you can set the value in the user object.

'field_user_account_terms' => array(
                'und' => array(
                    0 => array(
                        'value' =>  1,
                    ),
                ),
            ),    

Your Answer

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

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