What was easy is now hard as I work with the FAPI (though it does seem overall a lot better than by hand).
I have a table that stores user's subscribed content types. And I have a table that stores allowed Content Types users can subscribe to. So by querying the Content type table I get a checkbox list of items. But I also want to set the ones as checked that the user already has chosen.
Here is the form:
function buyctsubs_user_settings() {
//$form = array();
global $user;
/**
* create the node type array with ID and nice names
*
*/
$sql = "SELECT bt.type_id as TypeID, nt.name as Proper FROM {buyctsubs_type} as bt LEFT JOIN {node_type} as nt on nt.type LIKE bt.type_name ";
$result = db_query($sql);
while ($data = db_fetch_object($result)) {
$vars[$data->TypeID] = $data->Proper; //this builds the form from the table of allowed content types
}
/**
*Need to make a query to get and set users defauls
*This also prevents type from showing up that are not allowed
*/
$sql = "SELECT t.type_id as TypeID, nt.name as Proper
FROM {buyctsubs_type} AS t
LEFT JOIN {buyctsubs_user} AS u ON u.type_id LIKE t.type_id
LEFT JOIN {node_type} as nt on nt.type LIKE t.type_name
WHERE u.user_id = %d";
//echo $sql;
$result = db_query($sql, $user->uid);
while ($data = db_fetch_object($result)) {
$users_vars[] = $data->TypeID; // this sets the defaults in a way that is needed for below note it differs from above
}
$form['buyctsubs_user_settings'] = array(
'#type' => 'checkboxes',
'#title' => t('Users may subscribe these node types'),
'#options' => $vars, ***HERE IS ONE VARIABLE***
'#default_value' => $users_vars, ***HERE IS ANOTHER VARIABLE(see note below)***
'#description' => t('Choose as many as you want'),
);
$form['array_filter'] = array('#type' => 'hidden');
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Save')
);
if(isset($_POST[submit])){
drupal_set_message(t("Your settings have been saved"));
}
return $form;
// return system_settings_form($form);
}
That is it, now this form minus the ***HERE IS ONE VARIABLE*** callouts, makes a check list of preselected content types so the user know what they already have chosen and can remove or add to the list. I ran into trouble because I tried using the $users_vars[] = $data->TypeID like the $vars[$data->TypeID] = $data->Proper but after removing the first part of the array and just using the values value and not the name as well it was fine.