function optionwidgets_widget in Content Construction Kit (CCK) 5
Same name and namespace in other branches
- 6.3 modules/optionwidgets/optionwidgets.module \optionwidgets_widget()
- 6 modules/optionwidgets/optionwidgets.module \optionwidgets_widget()
- 6.2 modules/optionwidgets/optionwidgets.module \optionwidgets_widget()
Implementation of hook_widget().
File
- ./
optionwidgets.module, line 50 - Defines selection, check box and radio button widgets for text and numeric fields.
Code
function optionwidgets_widget($op, &$node, $field, &$items) {
switch ($op) {
case 'prepare form values':
$options = _optionwidgets_options($field, $node);
$items_transposed = content_transpose_array_rows_cols($items);
$values = isset($items_transposed['value']) && is_array($items_transposed['value']) ? $items_transposed['value'] : array();
$keys = array();
foreach ($values as $value) {
$key = array_search($value, array_keys($options));
if (isset($key)) {
$keys[] = $value;
}
}
if ($field['multiple'] || $field['widget']['type'] == 'options_onoff') {
$items['default keys'] = $keys;
}
else {
$items['default key'] = reset($keys);
}
break;
case 'form':
$options = _optionwidgets_options($field, $node);
$form = array();
$form[$field['field_name']] = array(
'#tree' => TRUE,
);
switch ($field['widget']['type']) {
case 'options_select':
if ($field['multiple']) {
$form[$field['field_name']]['keys'] = array(
'#type' => 'select',
'#title' => t($field['widget']['label']),
'#default_value' => $items['default keys'],
'#multiple' => TRUE,
'#size' => min(count($options), 6),
'#options' => $options,
'#required' => $field['required'],
'#description' => content_filter_xss(t($field['widget']['description'])),
);
}
else {
$form[$field['field_name']]['key'] = array(
'#type' => 'select',
'#title' => t($field['widget']['label']),
'#default_value' => $items['default key'],
'#multiple' => FALSE,
'#options' => $options,
'#required' => $field['required'],
'#description' => content_filter_xss(t($field['widget']['description'])),
);
}
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] == $on_value ? 1 : 0,
'#return_value' => $on_value,
'#description' => content_filter_xss(t($field['widget']['description'])),
'#required' => FALSE,
);
break;
case 'options_buttons':
if ($field['multiple']) {
$form[$field['field_name']]['keys'] = array(
'#type' => 'checkboxes',
'#title' => t($field['widget']['label']),
'#default_value' => $items['default keys'],
'#options' => $options,
'#required' => $field['required'],
'#description' => content_filter_xss(t($field['widget']['description'])),
);
}
else {
$form[$field['field_name']]['key'] = array(
'#type' => 'radios',
'#title' => t($field['widget']['label']),
'#default_value' => $items['default key'],
'#options' => $options,
'#required' => $field['required'],
'#description' => content_filter_xss(t($field['widget']['description'])),
);
}
break;
}
return $form;
case 'process form values':
$options = _optionwidgets_options($field, $node);
if ($field['multiple'] || $field['widget']['type'] == 'options_onoff') {
$keys = (array) $items['keys'];
}
else {
$keys = array(
$items['key'],
);
}
$values = array();
foreach ($keys as $key) {
if (isset($options[$key])) {
$values[] = $key;
}
}
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.
unset($items['keys']);
unset($items['key']);
break;
}
}