object_select_tag broken when using include_blank or include_custom and object has non-consecutive keys.
This is because include_blank and include_custom get array_unshifted onto the select_options array.
This re-indexes select_options, thus changing its keys (which is supposed to represent object Ids.
I have written a function to fix this and some other problems:
function my_object_select_tag($object, $method, $options = array(), $default_value = null)
{
$param_name = _convert_method_to_name($method, $options);
if (($reqvalue = _get_request_value($param_name)) !== null && $default_value === null)
{
$default_value = $reqvalue;
}
$options = _parse_attributes($options);
$related_class = isset($optionsrelated_class?) ? $optionsrelated_class? : ;
if (!isset($optionsrelated_class?) && preg_match('/get(.+?)Id$/', $method, $match))
{
$related_class = $match[1];
}
unset($optionsrelated_class?);
$select_options = _get_values_for_object_select_tag($object, $related_class);
$merge_array = array();
if (isset($optionsinclude_merge?))
{
$merge_array= $optionsinclude_merge?;
unset($optionsinclude_merge?);
}
if (isset($optionsinclude_custom?))
{
array_unshift($merge_array, $optionsinclude_custom?);
unset($optionsinclude_custom?);
}
else if (isset($optionsinclude_title?))
{
array_unshift($merge_array, '-- '._convert_method_to_name($method, $options).' --');
unset($optionsinclude_title?);
}
else if (isset($optionsinclude_blank?))
{
array_unshift($merge_array, );
unset($optionsinclude_blank?);
}
// ksort($select_options,SORT_NUMERIC);
if (!empty($merge_array))
{
$select_options = $merge_array+ $select_options;
}
$value = _get_object_value($object, $method, $default_value);
$option_tags = options_for_select($select_options, $value, $options);
return select_tag(_convert_method_to_name($method, $options), $option_tags, $options);
}