The short answer:

It’s not possible

The long answer:

In a simple user form it is possible to add a single on/off checkbox.

$form['thermometer_enabled'] = array(
'#type' => 'checkbox',
'#title' => t('Show the thermometer'),
'#options' => array(0 => t("no"), 1 => t("yes")),
);

But, it’s not possible to provide a default value (checked/unchecked) with a single chechbox. This causes difficulties when you want to show the saved value of a user.

A simple solution is to use a “yes/no radiobutton list” instead of a single checkbox.

$form['thermometer_enabled'] = array(
'#type' => 'radios',
'#title' => t('Show the thermometer'),
'#options' => array(0 => t("no"), 1 => t("yes")),
'#default_value' => variable_get('thermometer_enabled', 0),
);

In this example the result is saved in the variable thermomoeter_enabled and has the default value of 0 (false).


Rss Comments

Comments

No comments yet.

Leave a comment






Wordpress.org clearPaper by CreativeBits.it Copyright © 2012-2019 Robin Brackez. All rights reserved. By visiting this site you agree to accept cookies that are purely used to check how many visitors I have. Theme by: creativebits. Custom adaptations by Robin Brackez.