I have a minimal form with a single field to add an image to an entity and a save button. I have used managed_file
to add the image.
After adding the image from form the image gets saved in db.
Next time the user opens the form, it shows blank field and after adding a value current one gets replaced. What I want to do is user should see the existing value of the field when reopening the form.
How do I do this?
[Edit]:
Code for my form is:
// Form Definition
function custom_profile_form($form, &$form_state){
$form = array();
$form['picture'] = array(
'#title' => t('File'),
'#type' => 'managed_file',
'#description' => t('The uploaded file will be processed.'),
);
$form['buttons']['submit'] = array(
'#type' => 'submit',
'#value' => t('Import'),
);
return $form;
}
// Form Validation
function custom_profile_form_validate(&$form, &$form_state) {
$file = file_save_upload('picture');
$file->status = FILE_STATUS_PERMANENT;
$uploaded = file_save($file);
$upl = file_move($uploaded, "public://");
$form_state['storage']['fid'] = $upl->fid;
}
// Form Submission
function custom_profile_form_submit(&$form, &$form_state) {
global $user;
$user1 = user_load($user->uid);
$file = file_load($form_state['storage']['fid']);
$edit = array(
'picture' => $file,
);
user_save($user1, $edit);
}