Am using the DnDU module, paired with FileField and Upload.
I have implemented hook_file_insert(), and do some processing of the file. That's all working as expected. But, there are conditions, where I want to provide a URL, and navigate away, as soon as the file is finished uploading.
If I place a drupal_goto() at the end of mymodule_file_insert, it is 'trapped' inside of the javascript element. The page does not redirect, the HTML for only the drop box disappears, and is replaced with the text:
"Unspecified Error"
Question: Is it possible to call a redirect from PHP, or do I need to build a custom Javascript event handler?
Thanks!
Comments
Comment #1
decipheredHi craig_
As this is a heavily JavaScript based module the solution you are looking for is definitely a JavaScript solution, where it is to trigger an AJAX/AHAH callback to also use a PHP solution (seems unnecessary) or simply have JavaScript trigger the redirect.
However, no matter how you where implementing the upload, in the current version via a Node or in the future versions via any form, you will need to submit the form to make sure the uploaded file is stored in the database as a permanent file, so redirecting to a URL after upload is unlikely the path you should take. Instead, you may consider submitting the form automatically and have a $form['#redirect'] in the form, or a drupal_goto() in the form_submit/node_api.
To trigger the form submit (or redirect if you still choose to go down that path), you will want to define a JavaScript Drupal.behavior call that checks to see the file has been uploaded and then does whatever it needs to do.
If you need more pointers I will be happy to help, but take into account that what you are trying to achieve is not exactly simple, so it will need your time to actually make it work.
Hope this helped.
Cheers,
Deciphered.
Comment #2
craig_ commented-removed repeated post-
Comment #3
craig_ commentedHi Deciphered,
Appreciate the feedback.
I think I'm going to have to write an AJAX callback, or find a way to pass the custom URL to Javascript, and trigger the redirect.
Normally, your comments about using FAPI and processing the form, using [#redirect] would be the right approach. But, my requirements were to have a dropbox, where any user could:
1. drag/drop a file onto the browser,
2. the file would be automatically uploaded to the server,
3. some information would be extracted from the file contents,
4. a url would be constructed, using some of the contents,
5. the file would be discarded,
6. the user automatically would be redirected to the crafted url
DnDU + FileField + CCK + FAPI + Menu, gave me the hooks that I needed out of the box. But, we are not saving any nodes, or the files. So, I have run the following steps, but am stuck on the last step:
1. create a new content type with only one FileField field
2. added a hook_menu callback
3. call drupal_get_form, pushing in a new node of the new CCK type
4. run hook_alter_form, and strip all form elements, except for the FileField
5. implement hook_insert_file, and do the file processing there
6. call filefield_delete_file
7. craft a url with the processed information (needs to use drupal's l() )
8. immediately redirect the user to that url
Without hacking dragndrop_uploads.js, how and where can I pass the URL that I have created back to Javascript, for page redirection, and trigger it when file upload completes?
( Drupal.settings.dragNDropUploads.trigger == true; )
If you can point me towards how to pass the URL to javascript, and how to react to your .trigger before it gets reset to false ... that would be a huge help!
Thanks,
-Craig
Comment #4
decipheredHi Craig,
First thing I can say, you're going to love the next version of DnDU, the primary part of my development has been to remove the dependency on nodes, instead allowing a DnDU dropzone to be added to any page that has a file handler, which seems like it would remove the majority of the complexity of your usecase. However, it is not ready to be released even as a dev version.
Onto your questions:
Checking for Drupal.settings.dragNDropUploads.trigger will only work if your Drupal.behavior is called before Drupal.behavior.dragNDropUploads, as after that is run the value will be set back to null.
However, one benefit of JavaScript is that you can redefine JavaScript functions so you can takeover that function instead of causing a conflict. I took this approach in a similar sounding project, redefine Drupal.behavior.dragNDropUploads and have it trigger the URL redirect.
To get the URL, you have a few options, if you take over the behavior as suggested above, you could simply do a $.getJSON().
You could also attach a FileField formatter (see http://drupal.org/project/custom_formatters) that generates the URL (or calls the function that does) and returns the URL so that the value will be in a hidden field of your FileField.
Hope this helps.
Cheers,
Deciphered.