function select_or_other_process in Select (or other) 6
Same name and namespace in other branches
- 6.2 select_or_other.module \select_or_other_process()
Process callback for a Select (or other) element.
1 string reference to 'select_or_other_process'
- select_or_other_elements in ./
select_or_other.module - Implementation of hook_elements().
File
- ./
select_or_other.module, line 74 - The Select (or other) module.
Code
function select_or_other_process($element, $edit, &$form_state, $form) {
$element['#tree'] = TRUE;
$element['#processed'] = TRUE;
unset($element['#type']);
// Create the main select box
// Note that #title, #default_value, #disabled, #multiple, #options, #attributes,
// #required, and #size as passed to the select box from the main element.
$element['select'] = array(
'#type' => $element['#select_type'],
'#title' => $element['#title'],
'#default_value' => $element['#default_value'],
'#disabled' => $element['#disabled'],
'#multiple' => $element['#multiple'],
'#required' => $element['#required'],
'#size' => $element['#size'],
'#options' => $element['#options'],
'#attributes' => $element['#attributes'],
'#weight' => 10,
// The following values were set by the content module and need
// to be passed down to the nested element.
'#field_name' => $element['#field_name'],
'#delta' => $element['#delta'],
'#columns' => $element['#columns'],
);
// Remove the default value on the container level so it doesn't get rendered there.
$element['#value'] = NULL;
// Remove the required parameter so FAPI doesn't force us to fill in the textfield.
$element['#required'] = NULL;
// Allows other modules to update the #title_display options.
$element['#pre_render'] = array(
'_select_or_other_title_display',
);
// Now we must handle the default values.
$other_default = array();
// Easier to work with the defaults if they are an array.
if (!is_array($element['select']['#default_value'])) {
$element['select']['#default_value'] = array(
$element['select']['#default_value'],
);
}
// Process the default value.
foreach ($element['select']['#default_value'] as $key => $val) {
if ($val && isset($element['select']['#options']) && is_array($element['select']['#options']) && !array_key_exists($val, $element['select']['#options']) && !in_array($val, $element['select']['#options'])) {
// Not a valid option - add it to 'other'.
if ($element['#other_unknown_defaults'] == 'other') {
if ($element['#other_delimiter']) {
$other_default[] = $val;
}
else {
$other_default = array(
$val,
);
}
// Remove it from the select's default value.
unset($element['select']['#default_value'][$key]);
}
elseif ($element['#other_unknown_defaults'] == 'append') {
$element['select']['#options'][$val] = $val;
}
}
}
// If the expected default value is a string/integer, remove the array wrapper.
if ($element['#select_type'] == 'radios' || $element['#select_type'] == 'select' && !$element['#multiple']) {
$element['select']['#default_value'] = reset($element['select']['#default_value']);
}
$other_default_string = '';
if (!empty($other_default)) {
$other_default_string = implode($element['#other_delimiter'], $other_default);
if (is_array($element['select']['#default_value'])) {
$element['select']['#default_value'][] = 'select_or_other';
}
else {
$element['select']['#default_value'] = 'select_or_other';
}
}
// Add in the 'other' option.
$element['select']['#options']['select_or_other'] = $element['#other'];
// Create the 'other' textfield.
$element['other'] = array(
'#type' => 'textfield',
'#weight' => 20,
'#default_value' => $other_default_string,
'#disabled' => $element['#disabled'],
'#attributes' => $element['#attributes'],
);
// Populate properties set specifically as #select_property or #other_property
$sub_elements = array(
'select',
'other',
);
foreach ($sub_elements as $sub_element) {
foreach ($element as $key => $value) {
if (strpos($key, '#' . $sub_element . '_') === 0) {
$element[$sub_element][str_replace('#' . $sub_element . '_', '#', $key)] = $value;
}
}
// Also add in a custom class for each.
if (isset($element[$sub_element]['#attributes']['class'])) {
$element[$sub_element]['#attributes']['class'] .= " select-or-other-" . $sub_element;
}
else {
$element[$sub_element]['#attributes']['class'] = "select-or-other-" . $sub_element;
}
}
return $element;
}