2

I'm having a drupal form (7.x) of type checkboxes.

I have an array of values for my checkboxes.

$options = array(
    'uid'         => 'UID',
    'name'        => 'Name',
    'mail'        => 'E-mail',
    'created'     => 'Created',
    'edit_node'   => 'Edit',
    'cancel_node' => 'Cancel',
);

I have an array of default selected values.

$defaults = array(
    'uid'         => 'UID',
    'name'        => 'Name',
);

And now I try to make the default values selected by default, like this:

$form['dynamic_user_fields'] = array(
    '#type' => 'checkboxes',
    '#title' => t("Dynamic User Fields"),
    '#options' => $options,
    '#default_value' => $defaults,
    '#description' => t("Select the fields to display in the view."),
);

But this is not working, all checkboxes are unchecked by default. Where is my mistake?


What is not working so far: I've seen this related question but the answers do not work.

What I've tried:

(1) using numeric keys does not work.

'#default_value' => array(1, 2),

numeric keys

(2) using array keys does not work.

'#default_value' => array('uid', 'name'),

array keys

(3) using array structure does not work.

'#default_value' => array('uid' => 'UID', 'name' => 'Name')

array structure

Any ideas?


References

I stepped up to maintain this module but the code base is a migrated D6 module.

-1

For form elements expecting multi-value defaults, Drupal expects an array where keys and values are equal to the keys in your options array. Something like the following should work:

$defaults = array(
    'uid' => 'uid',
    'name' => 'name',
);

You may also use $defaults = drupal_map_assoc(array('uid', 'name'));.

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.