Tell me more ×
Drupal Answers is a question and answer site for Drupal developers and administrators. It's 100% free, no registration required.

So I am reading Drupal Development Pro 7 and have stumbled upon something I am a little bit unsure about to do with the variable_get function & form element. I understand it retrieves a stored value from the database and the second parameter is a default value if it cannot find anything in the database matching that form element.

My question is what does the second parameter do in this example below.

foreach($types as $node_type){
    $options[$node_type->type] = $node_type->name; //access the $node_type object and store specific properties from the node_type object in $options array
}

//Checkboxes on Form
$form['annotate_node_types'] = array(
    '#type' => 'checkboxes',
    '#title' => t('Users may annotate these content types'),
    '#options' => $options,
    '#default_value' => variable_get('annotate_node_types', array('page')), //when using the system_settings_form, the name of the form element must match the key of the variable_get ie $form[] = variable_get('$form[]', '') Always form_element name
    '#description' => t('A text field will be available on these content types to make user-specific notes.'),
);
share|improve this question

1 Answer

up vote 2 down vote accepted

The #default_value for a 'checkboxes' element must be an array rather than a single value (so you can have multiple default values selected).

All that code does is provide the default value wrapped in an array, so that it conforms to what the 'checkboxes' element type is expecting.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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