function select_or_other_widget in Select (or other) 6
Same name and namespace in other branches
- 6.2 select_or_other.module \select_or_other_widget()
Implementation of hook_widget().
This is a CCK hook.
File
- ./
select_or_other.module, line 345 - The Select (or other) module.
Code
function select_or_other_widget(&$form, &$form_state, $field, $items, $delta = NULL) {
$options = array();
// Create options - similar to code from content_allowed_values().
$list = explode("\n", $field['widget']['available_options']);
if (isset($field['widget']['available_options_php'])) {
ob_start();
$list = eval($field['widget']['available_options_php']);
ob_end_clean();
}
$list = array_map('trim', $list);
$list = array_filter($list, 'strlen');
foreach ($list as $opt) {
// Sanitize the user input with a permissive filter.
$opt = content_filter_xss($opt);
if (strpos($opt, '|') !== FALSE) {
list($key, $value) = explode('|', $opt);
$options[$key] = isset($value) && $value !== '' ? $value : $key;
}
else {
$options[$opt] = $opt;
}
}
// Add a 'none' option for non-required single selects, sort selects, and radios
if (!$field['required']) {
if ($field['widget']['type'] == 'select_or_other_buttons' && !$field['multiple'] || $field['widget']['type'] == 'select_or_other' && !$field['multiple'] || $field['widget']['type'] == 'select_or_other_sort') {
$options = array(
'' => theme('select_or_other_none', $field),
) + $options;
}
}
// Construct the element.
$element = array(
'#type' => 'select_or_other',
'#other' => $field['widget']['other'],
'#default_value' => isset($items[$delta]) ? $items[$delta] : NULL,
'#options' => $options,
'#description' => $field['widget']['description'],
'#multiple' => $field['multiple'],
'#required' => $field['required'],
//'#other_delimiter' => $field['widget']['other_delimiter'] == 'FALSE' ? FALSE : $field['widget']['other_delimiter'],
'#other_delimiter' => FALSE,
'#other_unknown_defaults' => $field['widget']['other_unknown_defaults'],
'#element_validate' => array(
'select_or_other_cck_validate',
),
'#cck_widget' => $field['widget']['type'],
);
// Set select type's.
switch ($field['widget']['type']) {
case 'select_or_other':
$element['#select_type'] = 'select';
break;
case 'select_or_other_sort':
$element['#select_type'] = 'select';
// Also disable multiples for this select type.
$element['#multiple'] = FALSE;
break;
case 'select_or_other_buttons':
$element['#select_type'] = $field['multiple'] ? 'checkboxes' : 'radios';
break;
}
// In situations where we handle our own multiples (checkboxes and multiple selects) set defaults differently.
if ($element['#multiple']) {
$element['#default_value'] = array();
foreach ($items as $delta => $item) {
$element['#default_value'][$delta] = $item['value'];
}
}
return $element;
}