10

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).

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

2 Answers 2

20

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'.

2
  • 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
    Commented Jul 29, 2013 at 11:08
  • 1
    @vr3690 Nope, you didn't set default value. See comment under your question, or update to my answer.
    – Mołot
    Commented Jul 29, 2013 at 11:11
11

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,
);
4
  • 3
    Hi, I am already using similar code. I have edited my answer and added the same to it. This does not work.
    – bcosynot
    Commented Jul 29, 2013 at 11:04
  • 2
    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
    Commented Jul 29, 2013 at 11:15
  • 1
    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 Commented Nov 20, 2018 at 20:02
  • 1
    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
    Commented Nov 20, 2018 at 20:12

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