0

I am using managed_file in Drupal 7 for my custom module. Other than the form api and managed_file, I'm using my own custom tables for this module.

This will eventually be used on shared hosting via Dreamhost, so the requirements of using PECL extensions are probably not available at this answer suggests.

The behavior I want is when the file is uploaded by clicking the "upload" button, I at least want the image file to be a link that appears to indicate to the user that the file is uploaded. When I left the default #progress_indicator in place, the AJAX indicates it never uploads. However, this is false as I can see the file in managed_file table in the db.

I've looked through several searches for this and this is the form that I've come up and I cannot see what I'm doing wrong.

$item is the result of a database call of the previous object. Its genesis was derived from a post jaypan provided.

$form['#item'] = $item;

dpm($item); => //every attribute to the sponsor object in the database.

$uri = isset($item->logoFileID) ? fetch_name('file_managed', 'uri', 'fid', $item->logoFileID) : '';

$uri = isset($item->logoFileID) ? fetch_name('file_managed', 'uri', 'fid', $item->logoFileID) : '';
dpm($uri); => 'public://logos/myfile.gif' //to show valid return of $uri
dpm($item-logoFileID); => 'public://logos/myfile.gif'

  $form['logoFileID'] = array(
    '#title' => 'file key',
    '#type' => 'hidden',
    '#default_value' => $item->logoFileID,
    );
 $form['logo_fid'] = array(
    '#type' => 'managed_file',
    '#title' => t('Logo'),
    '#description' => t('Upload a file, allowed extensions: jpg, jpeg, png, gif'),
    '#upload_location' => 'public://logos/',
    '#progress_indicator' => 'none',
    '#default_value' => $file->uri,
    );

As I said, managed_file does save the file and returns an fid that I save with the sponsor record. The behavior I have gotten in this module is that the filename was showing in this part of a form, that when clicked in Drupal shows a popup modal of the image. My question is how do I get this back link back to appear in the form to show the user it has been uploaded?, thx sam

2

The value of a managed file field is a file ID, rather than its URI.

Assuming $file in your code is a file object, change the element array to:

$form['logo_fid'] = array(
  '#type' => 'managed_file',
  '#title' => t('Logo'),
  '#description' => t('Upload a file, allowed extensions: jpg, jpeg, png, gif'),
  '#upload_location' => 'public://logos/',
  '#progress_indicator' => 'none',
  '#default_value' => $file->fid,
);
  • Yes, you are certainly right! – sam452 Aug 31 '15 at 19:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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