Flags drupal form api

I am using drupal api form and using checkboxes. I am having problems with the default values ​​checked. Below is a snippet of code ...

$result = db_query("SELECT nid, filepath FROM {content_type_brand}, {files} WHERE content_type_brand.field_brand_image_fid = files.fid"); $items = array(); while ($r = db_fetch_array($result)) { array_push($items, $r); } $options = array(); foreach( $items as $i ) { $imagePath = base_path().$i['filepath']; $options[$i['nid']] = '<img src="'.$imagePath.'"></img>'; } $form['favorite_brands'] = array ( '#type' => 'fieldset', '#title' => t('Favorite Brands'), //'#weight' => 5, '#collapsible' => TRUE, '#collapsed' => FALSE, ); $form['favorite_brands']['brands_options'] = array( '#type' => 'checkboxes', '#options' => $options, '#default_value' => $options_checked,// $options_checked is an array similar to $options but having elements which need to be checked by default... '#multicolumn' => array('width' => 3) ); 

but the default values ​​are not checked ... can anyone help with what I am missing?

thanks

+7
source share
1 answer

Your $ options_checked array should not be in the same format as your $ options array. Your $ options array contains nid => img pairs. Your $ options_checked array should simply contain the nid values ​​of the options, which should be checked by default:

 $options_checked = array(8,17); 
+7
source

All Articles