How to modify forms and set defaults
Submitted by valerio on Thu, 02/05/2009 - 00:48
In many cases like with Organic Groups, you want to remove some fields, make tham invisible and set defaults.
The best approach is to create a site-specific module and write a hook_form_alter function. Do a print_r($form) to find out the form structure and then unset the fields that you want to zap or change the fields properties.
To check a checkbox use $form['og_private']['#attributes'] = array('checked' => '1');
Example:
function yoursite_form_alter(&$form, $form_state, $form_id) {
global $user;
// echo "form_id = $form_id <br/>";
// print_r($form);
// foreach ($form as $key => $value){
// echo "$key - $value <br/>";
// }
// Set the defaults for the group creation.
if ($form_id == 'customer_node_form') {
unset($form['og_selective']['#options'][1]); // hide Moderated option
unset($form['og_selective']['#options'][2]); // hide Invote only option
unset($form['og_directory']); // hide List in groups directory
unset($form['og_register']); // hide Registration form
// Check "Private group" and disable it.
$form['og_private']['#attributes'] = array('checked' => '1');
$form['og_private']['#disabled'] = 1;
}
}
One issue that you might encounter is that if you want to assign specific properties to radio button you'll find that in the hook_form_alter function they haven't been expanded yet. The solution is to register a funtion that is called after the expansion.
function yourtheme_form_alter(&$form, $form_state, $form_id) {
$form['og_selective']['#after_build'][] = 'yourtheme_select_closed_group';
...
}
function yourtheme_select_closed_group($element) {
// the radio has been expanded by now
$element[3]['#attributes'] = array('checked' => '1');
return $element;
}
- Login to post comments