Upload and preview

drewish - December 6, 2005 - 10:31
Project:Filemanager
Version:HEAD
Component:Code
Category:support request
Priority:normal
Assigned:Unassigned
Status:postponed
Description

I'm wondering if anyone can provide a pointer some code that uses the filemanager to allow files to be previewed and then saved. I'll contrast this, desired behavior, with that of the project module. If I was to attach a file to the bottom of this bug report and click preview, it would upload but be lost by the time I clicked post.

Is there a prefered way to upload the file, save it as a working copy during the validate/preview and then hold onto the fid long enough to promote that file to the active copy when the node is saved?

Hopefully that makes sense, I've been up too long,
andrew

#1

vhmauery - December 6, 2005 - 17:54

Acidfree now supports previews, which means it has to keep ahold of the fid for long enough to promote it to working. I am not really proud of this part of the code, but it does seem to be working ATM.

#2

drewish - December 7, 2005 - 20:01

yeah, that is some pretty convoluted code. the attach module does it pretty well but they use the hook_nodeapi'a prepare op to grab uploads before the copy is forked for previewing. maybe drupal needs another node hook to allow nodes a chance to do the same thing.

#3

drewish - December 19, 2005 - 14:26

Well, I finally did some hacking and figured out one way to do it.

Your node's table will need to track the file id and you'll need to make sure it's assigned to the node as part of the hook_load() implementation.

On the node's form you'll need a hidden element to store the fid.

The real magic happens in your implementation of hook_prepare:

function hook_prepare(&$node) {
  // figure out if there's a file attached to this node
  if ($node->fid) {
    // has a existing file, editing an existing node, re-use the file.
    $file = filemanager_get_working_copy($node->fid, FALSE);
  } elseif (isset($_POST['edit']['fid'])) {
    // this is a preview (on a new or edit)
    $file = filemanager_get_file_info($_POST['edit']['fid']);
  } else {
    // this is a new node, create a new file
    $file = FALSE;
  }

  if ($upload = file_check_upload('NAME_OF_FORM_FILE_ELEMENT')) {
    $file = filemanager_add_upload($upload , 'FILEMANAGER_AREA', FALSE, $file);
    // read any input you need from the file. any data you read needs to be saved
    // to both the node and POST if you want it to appear in the preview and be
    // set on the form.
    $node->title = $_POST['edit']['title'] = $file->filename;
  }

  if ($file) {
    // save the new file info back to the node, you may need to assign it to
    // $_POST['edit']['fid'] as well
    $node->fid = $file->fid;
  }
}

When you save your node (hook_insert()/hook_update()) you'll want to do something like:

  $file = filemanager_get_working_copy($node->fid, FALSE);
  $file = filemanager_promote_working($file);

Your hook_delete() will need:

  filemanager_delete(filemanager_get_file_info($node->fid));

If anyone's interested, I'll write up a simple example module.

#4

drewish - December 20, 2005 - 20:05

Actually, the more I think about it, it's probably a bad idea to put the fid on the form. You open yourself up to all sorts of mischeif. It's probably better to hide it in $_SESSION or something like that.

#5

vhmauery - December 21, 2005 - 00:13

If you are dealing with 4.7 forms, they say you can get around using hidden fields at all.

2. If a form has an submit function, then hidden form values are not needed. Instead, any values that you need to pass to $form_values can be declared in the $form array as such:

      <?php
      $form
['foo'] = array('#type' => 'value', '#value' => 'bar')
     
?>

This is accessed in $form_values['foo'], with a value of bar. This method is preferred because the values are not sent to the browser.

The above quotation is located at the end of the forms api document. See http://drupaldocs.org/api/head/file/contributions/docs/developer/topics/... for more details.

#6

drewish - December 21, 2005 - 12:18

vhmauery, it's all for 4.7 (i actually don't think there's a hook_prepare in 4.6) but the problem with the values is that they're not passed in to the hook_prepare, only to the later hooks like submit/validate/insert/update, etc. so you wouldn't have the file id early enough to be able to process the uploaded file, load data from the file, an have it appear on both the form and in the preview.

#7

drewish - January 9, 2006 - 20:37

I've got the upload working with the session variable. If you're interested, take a look at the audio module I'm working on in my sandbox: http://cvs.drupal.org/viewcvs/drupal/contributions/sandbox/drewish/audio/

#8

ccourtne - February 16, 2006 - 03:51
Status:active» postponed

This doesn't actually seem to be a filemanager issue per say. I will leave in postponed state right now as I believe it will generate some documentation. I need to upload the attachment module this evening and I will try to document how to interact with dynamic uploads.

 
 

Drupal is a registered trademark of Dries Buytaert.