Drupal 6 Checkboxes and default_value
The Drupal Forms API comes with some powerful features, including checkboxes which allows a bunch of checkbox options to be grouped together. Included in this functionality is the ability to set default values, i.e. set your chosen boxes to be on.
However, it is not immediately obvious how to programmatically set the default values using the '#default_value' key. The key expects an array, so I tried the usual contenders such as an array of 0s, 1s, 'on', FALSE, TRUE etc etc and just couldn't get it to work correctly.
The answer is a little cryptic. Firstly, lets have a look at the checkboxes options. The options need to be presented to the '#options' key as an associative array with key / value pairings. For our trivial example, we would go with:
<?php
'#options' => array('apples' => 'apples', 'oranges' => 'oranges', 'bananas' => 'bananas', 'grapefruit' => 'grapefruit'),
?>Now the '#default_value'. The default values need to be presented as a regular index like:
<?php
'#default_value' => array('apples', 'oranges'),
?>This would tick the apples and the oranges boxes, whilst the bananas and grapefruit boxes would be unchecked.
In real life you'd probably want something a little more sophisticated than this - for instance saving the values in the variable table would be convenient way of retrieving the settings from the previous time the code was run. So instead of the above example, you'd do:
<?php
'#default_value' => variable_get('tester_admin_input_field_values', array(NULL)),
?>This would get the values from the table, providing they had been previously saved with
<?php
variable_set('tester_admin_input_field_values', $form_state['values']['tester_optional_fields']);
?>Note that when you use variable_get, you must provide a default value that needs to be an array or else you will get an error message first time through the code when no value is currently saved in the database. That is why I have passed an array with NULL to the function.
Ok, so below is the entire code you can cut and paste into your own application.
tester_admin_input_field.inc
<?php
// Tester for checkboxes and default_value
function tester_admin_input_field() {
$form['tester_optional_fields'] = array(
'#type' => 'checkboxes',
'#title' => t('Input Fields'),
'#options' => array('apples' => 'apples', 'oranges' => 'oranges', 'bananas' => 'bananas', 'grapefruit' => 'grapefruit'),
'#description' => t("Check or uncheck the respective fields"),
'#default_value' => variable_get('tester_admin_input_field_values', array(NULL)),
//'#default_value' => array('apples', 'oranges'),
);
$form['#submit'][] = 'tester_admin_input_field_submit';
return system_settings_form($form);
}
function tester_admin_input_field_submit($form, $form_state) {
variable_set('tester_admin_input_field_values', $form_state['values']['tester_optional_fields']);
}
?>For completeness in case you are totally new to Drupal, here's the supporting module code.
tester.module
<?php
// Tester module for trying stuff out on a clean Drupal install
/**
* Inplements hook_node_info()
**/
function tester_node_info() {
// stub
}
/**
* Inplements hook_access()
**/
function tester_access($op, $node, $account) {
// stub
}
/**
* Inplements hook_access()
**/
function tester_menu() {
$items = array();
$items['admin/tester'] = array(
'title' => 'Tester Module',
'description' => 'Adjust Tester Settings',
'position' => 'right',
'weight' => -5,
'page callback' => 'system_admin_menu_block_page',
'access arguments' => array('administer site configuration'),
'file' => 'system.admin.inc',
'file path' => drupal_get_path('module', 'system'),
);
$items['admin/tester/settings'] = array(
'title' => 'Tester Input Settings',
'description' => 'Facility to change module settings using checkboxes',
'weight' => -10,
'page callback' => 'drupal_get_form',
'page arguments' => array('tester_admin_input_field'),
'access arguments' => array('administer site configuration'),
'type' => MENU_NORMAL_ITEM,
'file' => 'tester_admin_input_field.inc',
);
return $items;
}
?>tester.info
name = "Tester"
decription = "Clean Drupal Install Tester Module For Trying Stuff Out"
core = 6.x
php 5.2