Pre-Populating a Form Field with something other than its default value I'm having a strange problem. I am constructing a form with checkboxes to toggle email subscription preferences on and off. For the new user sign-up form, this is straightforward:

$form['marketing-container']['restaurant_newsletter_subscription'] = array(
    '#type' =&gt; 'checkbox',
    '#title' =&gt; t('Subscribe to Restaurant Newsletter'),
    '#return_value' =&gt; "True",
    '#default_value' =&gt; "False",
);


However, when it comes to the update user form, I'm running into a problem. If I set #default_value to the existing preference, it will check the boxes for those publications you're subscribed to correctly, but if you UNcheck those boxes, the form API just returns the default value....which would still be TRUE.

To work around this, I retrieved the subscriber object at the start of the form function, and I've added hidden fields whose default values are the existing subscription preference values, just like I attempted to do with the real fields in the first place. Then, I used #states to populate the real fields based on these hidden fields, like so:

$form['marketing-container']['existing_restaurant_newsletter_subscription'] = array(
    '#type' =&gt; 'checkbox',
    '#title' =&gt; t('Restaurant Newsletter'),
    '#value' =&gt; $attributes[15]-&gt;Value,
);

$form['marketing-container']['restaurant_newsletter_subscription'] = array(
    '#type' =&gt; 'checkbox',
    '#title' =&gt; t('Subscribe to Restaurant Newsletter'),
    '#return_value' =&gt; "True",
    '#default_value' =&gt; "False",
    '#states' =&gt; array(
        'checked' =&gt; array(
            ':input[name="existing_restaurant_newsletter_subscription"]' =&gt; array('checked' =&gt; TRUE)
        )
    )
);


Now this works! The problem is that the first field is visible. Now if I alter this to hide the field:

$form['marketing-container']['existing_restaurant_newsletter_subscription'] = array(
    '#type' =&gt; 'checkbox',
    '#title' =&gt; t('Restaurant Newsletter'),
    '#value' =&gt; $attributes[15]-&gt;Value,
    '#access' =&gt; FALSE,
);


The field is hidden perfectly....but its value isn't copied over to the real field that's still visible.

I've searched all over and pored over the Form API documentation, and I'm stumped.

Is there some way to preserve this default value inside the hidden form so it copies over to another field correctly?

If there isn't, or if it's more efficient, is there a DIFFERENT method I could use to pre-check this checkbox but still keep a default value of FALSE so that if a user unchecks it, something actually happens? Is there another class of field I'm missing that is a sort of on-off switch that returns specified values for each state?
Figured it out on my own. The solution was:

$form['restaurant_newsletter_subscription'] = array(
    '#type' =&gt; 'checkbox',
    '#title' =&gt; t('Subscribe to Restaurant Newsletter'),
    '#return_value' =&gt; "True",
    '#default_value' =&gt; $attributes[15]-&gt;Value,
    '#prefix' =&gt; '&lt;div class="subscription-toggle"&gt;',
    '#suffix' =&gt; '&lt;/div&gt;',
    '#ajax' =&gt; array(
        'callback' =&gt; 'ajax_replace_default_callback',
        'wrapper' =&gt; 'restaurant_newsletter_subscription_div',
    ),
);


And for the callback:

function ajax_replace_default($form, $form_state) {
    $field_name = $form_state['triggering_element']['#name'];
    $form[$field_name]['#default_value'] = "False";
    return $form[$field_name];
}


And I also kept the hidden fields in the form:

$form['existing_restaurant_newsletter_subscription'] = array(
    '#type' =&gt; 'checkbox',
    '#title' =&gt; t('Restaurant Newsletter'),
    '#value' =&gt; $attributes[15]-&gt;Value,
    '#access' =&gt; FALSE,
);


This was simpler to store the existing preference variables for comparison in my logic later on than to try to retrieve the subscriber yet again inside of that form submit handler (It already retrieved the subscriber when it loaded the form, to populate the fields....trying to minimize the number of API calls I have to make).

Implemented like this, if a box is checked and becomes unchecked, that Ajax callback function alters the default value to false, rather than returning just the pre-populated value. If you check the box again it still changes the default to false, but it doesn't matter since if the box is checked that's irrelevant.

Not the most elegant fix, you get a little loading circle thing for a second when the Ajax runs, but it works.

Note: I left the Ajax callback function generic because even though I keep using the same field on this page, there are lots of similar fields doing the same thing. Using the triggering_element allows me to just recycle this code for all of them.