function select_or_other_element_process in Select (or other) 7.2
Same name and namespace in other branches
- 7.3 select_or_other.module \select_or_other_element_process()
Process callback for a Select (or other) element.
1 string reference to 'select_or_other_element_process'
- select_or_other_element_info in ./
select_or_other.module - Implements hook_element_info().
File
- ./
select_or_other.module, line 78 - The Select (or other) module.
Code
function select_or_other_element_process($element, &$form_state) {
$element['#tree'] = TRUE;
$element['#processed'] = TRUE;
// Load the JS file to hide/show the 'other' box when needed.
$element['#attached']['js'][] = drupal_get_path('module', 'select_or_other') . '/select_or_other.js';
// Create the main select box
// Note that #title, #title_display, #default_value, #disabled, #multiple,
// #required, #size, #options, and #attributes are passed to the select box
// from the main element automatically.
$element['select'] = array(
'#type' => $element['#select_type'],
'#title' => $element['#title'],
'#title_display' => $element['#title_display'],
'#default_value' => !empty($element['#default_value']) ? $element['#default_value'] : NULL,
'#disabled' => $element['#disabled'],
'#multiple' => $element['#multiple'],
'#required' => $element['#required'],
'#size' => isset($element['#size']) ? $element['#size'] : NULL,
'#options' => $element['#options'],
'#attributes' => $element['#attributes'],
'#weight' => 10,
);
foreach (array(
'#empty_option',
'#empty_value',
) as $key) {
if (isset($element[$key])) {
$element['select'][$key] = $element[$key];
}
}
// 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;
// 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']) && !select_or_other_multi_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['#other_unknown_defaults'] == 'available') {
$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 without the required attribute, if any.
$element['other'] = array(
'#type' => 'textfield',
'#weight' => 20,
'#default_value' => $other_default_string,
'#disabled' => $element['#disabled'],
'#attributes' => array_diff_key($element['#attributes'], array(
'required' => NULL,
)),
);
// 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.
$element[$sub_element]['#attributes']['class'][] = 'select-or-other-' . $sub_element;
}
if (!empty($element['#maxlength'])) {
$element['other']['#maxlength'] = $element['#maxlength'];
}
if (isset($element['#other_size'])) {
$element['other']['#size'] = $element['#other_size'];
}
// Hide the title from the wrapper.
$element['#title'] = NULL;
return $element;
}