8

I am using an checkboxes element within a form in module that I started developing for Drupal 7. This is my first attempt at module development and using the Form API. I am creating a list of checkboxes that list down all the content types of a specific site. An array of all the content types can be obtained by using the following: $nodeTypesRaw=node_type_get_names();. An array with the machine names of the content types as the keys and the human readable names as the values are obtained.

By default, I would like all the checkboxes to be selected and when the form is submitted. How do I achieve that? The Form API pages on the drupal site aren't very helpful with regards to setting the default values for checkboxes.

$nodeTypesRaw=node_type_get_names();
$nodeMachineNames=array_keys($nodeTypesRaw);
$form['process_comments']['custommod_published_comments_select_node_types']['custommod_published_comments_node_types_checkboxes']=array(
          '#deafult_value'=>$nodeMachineNames,
          '#options'=>$nodeTypesRaw,
          '#type'=>'checkboxes',
);

I initially started out with the above code, but it won't work. It does display the list of checkboxes, but they are not checked by default. The variable does get on form submission though (checked via drush and var_dump).

closed as off-topic by kiamlaluno May 27 '14 at 20:55

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "This question was caused by a problem that can no longer be reproduced, was solved by a cache clear, or was a simple typographical error. While similar questions may be on-topic here, this one was resolved in a manner unlikely to help future readers." – kiamlaluno
If this question can be reworded to fit the rules in the help center, please edit the question.

  • 2
    It's #default_value, not #deafult_value. FA not AF. – Mołot Jul 29 '13 at 11:08
15

From Drupal.org:

$form['element_id'] = array(
  '#type' => 'checkboxes',
  '#options' => array(1 => "One", 2 => "Two", 3 => "Three"),
  '#default_value' => array(1, 3),
);

Default value is set as an array representing keys of selected options.

Looking at the code in your question, problem is with spelling. you are setting '#deafult_value' when you want to set '#default_value'.

  • An array of all the content types can be obtained by using the following: $nodeTypesRaw=node_type_get_names();. An array with the machine names of the content types as the keys and the human readable names as the values are obtained. When I set the '#default_value` using the variable $nodeMachineNames=array_keys($nodeTypesRaw);, the checkboxes remain un-checked. – bcosynot Jul 29 '13 at 11:08
  • 1
    @vr3690 Nope, you didn't set default value. See comment under your question, or update to my answer. – Mołot Jul 29 '13 at 11:11
10

You need to give an array of option keys (not values) as the default value, e.g.

$options = array(
  'option_1' => t('Option 1'),
  'option_2' => t('Option 2'),
  'option_3' => t('Option 3'),
);

$defaults = array_keys($options);

$form['checkboxes'] = array(
  '#type' => 'checkboxes',
  '#title' => t('Something'),
  '#options' => $options,
  '#default_value' => $defaults,
);
  • Hi, I am already using similar code. I have edited my answer and added the same to it. This does not work. – bcosynot Jul 29 '13 at 11:04
  • 1
    Works fine for me in a blank install, are you sure you haven't got a typo in #default_value like @Mołot suggests – Clive Jul 29 '13 at 11:15
  • Just wondering, did it change at some point? Because I'm looking right now at form_process_checkbox and form_process_checkboxes of Drupal 7 and there is no mechanism that would allow for this to work, and of course it doesn't work for me. github.com/drupal/drupal/blob/7.x/includes/form.inc#L3270-L3351 – Łukasz Zaroda Nov 20 '18 at 20:02
  • Not that I know of @ŁukaszZaroda...if you look at user_account_form for example, the same method is used there for the roles – Clive Nov 20 '18 at 20:12

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