I'm trying to create a form using the Form API with the following behavior:
For a custom slider module, users can choose to upload an image which is saved as a managed file. This works fine. They also have the option to populate the slide content (slide title,link and image) from an existing node of a custom type. The node is selected from an auto-populated select field which on change calls an ajax function to auto-populate the title and subtitle fields. If the selected node has an image attached to it, a checkbox is displayed allowing the user to use the already uploaded image associated with node as the slider image as well. The problem I have is that I can't get the manged_file field to accept the nodes file id (fid) and re-render as managed_file with a default_value before returning it from the callback.
Here's my callback:
function load_image_callback($form,&$form_state) {
// Load node from the selected value
$node = node_load($form_state['values']['slide_nid'];
// Set the managed_file default value to the nodes main image file
$form_state['complete form']['slide']['slide_image']['slide_fid']['#default_value'] = $node->field_main_image[$node->language][0]['fid'];
// Return the updated slide image wrapper to replace the existing one
return $form_state['complete form']['slide']['slide_image'];
}