0

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);
}
2
  • "I have a minimal form with a single field" - can we see it?
    – Mołot
    Commented Dec 3, 2013 at 8:48
  • @Mołot: Added the code
    – kmdhrm
    Commented Dec 3, 2013 at 10:30

1 Answer 1

0

You can use #default_value attribute of the managed_file to show the existing value of the field.

You can get the value of fid from database and set value into #default_value.

Example:

$form['picture'] = array(
  '#title' => t('Image'),
  '#type' => 'managed_file',
  '#description' => '',
  '#default_value' => 1, //fid from database
  '#upload_location' => 'public://image_example_images/',
);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.