How to show the uploaded managed_file in the edit page?

751 views Asked by At

I am able to successfully upload files using managed_file. The file is saved in the correct directory and the record shows up in the file_managed table.

$form['image'] = array(
    '#title' => t('Image'),
    '#type' => 'managed_file',
    '#default_value' => (isset($foo->image->fid) ? $foo->image->fid : ''),
    '#upload_location' => 'public://foo_images/'
);

But when I edit the form, it still shows the Upload button, similar to the Create page before I have uploaded a file. How do I make the edit page look like I have already uploaded a file for that particular entity?

1

There are 1 answers

0
Randell On BEST ANSWER

Inspecting the $foo variable showed that I was supposed to use just $foo->image instead of $foo->image->fid.

Probably because I have the following lines in my _form_submit:

if (isset($form_state['values']['image'])) {
    $file = file_load($form_state['values']['image']); 
    $file->status = FILE_STATUS_PERMANENT;
    file_save($file);
    file_usage_add($file, 'foo', 'foo', $foo->tkid);

    $foo->image = $file->fid;
  }

Notice the part that says $foo->image = $file->fid;