Commit dbe29452 authored by KarenS's avatar KarenS

#111129, add widget to handle single on/off checkbox

parent 52625779
......@@ -31,6 +31,10 @@ function optionwidgets_widget_info() {
'label' => 'Check boxes/radio buttons',
'field types' => $option_types,
),
'options_onoff' => array(
'label' => 'Single on/off checkbox',
'field types' => $option_types,
),
);
}
......@@ -41,7 +45,13 @@ function optionwidgets_widget_settings($op, $widget) {
$form = array();
switch ($op) {
case 'form':
$form['#prefix'] = t('Create a list of options as a list in Allowed values or as an array in Php code at the bottom of this page. These values will be the same for the %field in all content types. The Check boxes/radio buttons widget will display checkboxes if the multiple values option is selected for this field, otherwise radios will be displayed.', array('%field' => $widget['label']));
$form['#prefix'] = t('Create a list of options as a list in <strong>Allowed values</strong> or as an array in Php code at the bottom of this page. These values will be the same for the %field in all content types. ', array('%field' => $widget['label']));
if ($widget['type'] == 'options_onoff') {
$form['#prefix'] .= t(' For a single on/off checkbox, define the \'off\' value first, then the \'on\' value in the <strong>Allowed values</strong> section.');
}
else {
$form['#prefix'] .= t(' The Check boxes/radio buttons widget will display checkboxes if the multiple values option is selected for this field, otherwise radios will be displayed.');
}
}
return $form;
}
......@@ -63,7 +73,7 @@ function optionwidgets_widget($op, &$node, $field, &$items) {
$keys[] = $value;
}
}
if ($field['multiple']) {
if ($field['multiple'] || $field['widget']['type'] == 'options_onoff') {
$items['default keys'] = $keys;
}
else {
......@@ -106,6 +116,20 @@ function optionwidgets_widget($op, &$node, $field, &$items) {
}
break;
case 'options_onoff':
// Display only the 'On' value of $options;
$vals = array_keys($options);
$on_value = $vals[1];
$form[$field['field_name']]['keys'] = array(
'#type' => 'checkbox',
'#title' => $options[$on_value],
'#default_value' => $items['default keys'][0],
'#return_value' => $on_value,
'#description' => $field['widget']['description'],
'#required' => FALSE,
);
break;
case 'options_buttons':
if ($field['multiple']) {
$form[$field['field_name']]['keys'] = array(
......@@ -130,14 +154,13 @@ function optionwidgets_widget($op, &$node, $field, &$items) {
}
break;
}
return $form;
case 'process form values':
$options = _optionwidgets_options($field);
if ($field['multiple']) {
$keys = $items['keys'];
if ($field['multiple'] || $field['widget']['type'] == 'options_onoff') {
$keys = (array) $items['keys'];
}
else {
$keys = array($items['key']);
......@@ -150,6 +173,11 @@ function optionwidgets_widget($op, &$node, $field, &$items) {
}
}
if ($field['widget']['type'] == 'options_onoff' && empty($values)) {
$keys = array_keys($options);
$values[] = $keys[0];
}
$items = content_transpose_array_rows_cols(array('value' => $values));
// Remove the widget's data representation so it isn't saved.
......@@ -183,4 +211,4 @@ function theme_optionwidgets_none($widget_type, $field_name, $node_type) {
default :
return '';
}
}
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment