Closed (fixed)
Project:
Drupal core
Version:
7.x-dev
Component:
documentation
Priority:
Normal
Category:
Bug report
Assigned:
Reporter:
Created:
23 Mar 2010 at 03:57 UTC
Updated:
20 Apr 2012 at 21:24 UTC
Jump to comment: Most recent file
The new #managed_file element provided by file.module is currently not documented on the Forms API reference page.
http://api.drupal.org/api/drupal/developer--topics--forms_api_reference....
| Comment | File | Size | Author |
|---|---|---|---|
| #7 | 750190.patch | 5.84 KB | jgraham |
Comments
Comment #1
jhodgdonI would like to postpone this until we decide to fix or not fix:
#100680: [meta] Make API module generate Form API documentation
because hopefully we'll be generating the Forms API reference automatically from comments in the code, instead of the current manual editing process.
Comment #2
rfayI will try to take this on. We need to have all of the FAPI documented without waiting for dependencies that may or may not arrive.
Comment #3
rfay@eojthebrave: If you can give me a quick set of pointers on how it's used, I'll write it up.
Comment #4
eojthebraveOkay, here we go.
The element type is
#managed_fileand provides you with a complete ajax/progress aware widget for uploading a file and saving it to the {files} table.By default you get a simple upload button, that allows you to choose a file to upload, once a file has been chosen you can click "Upload" and it will upload the file via ajax and display a progress meter. Your FAPI validate/submit callback will be given a file object ID that represents the ID of the new file in the {files} table.
The
#managed_fileelement is expanded into a set of FAPI elements including two '#submit' buttons one for Upload one for Remove, a '#file' element and a handful of '#hidden' and '#markup' elements to handle progress indication and displaying of already uploaded files.Note: New files are uploaded with a status of 0 and are treated as temporary files which are removed after 6 hours via cron. Your module is responsible for changing the $file objects status to FILE_STATUS_PERMANENT and saving the new status to the database. Something like the following within your submit handler should do the trick.
Once a file has been uploaded you can use the file objects fid as the
#default_valuefor the form element. This will display an icon, a link to the file, and a remove button.Clicking the remove button sets the value of the field to 0 and your module is responsible for actually removing the file from the files table and the file system. @see file_delete(). Off the top of my head I believe the $file object in question is stored somewhere in the $form_state array so you can access it without know the files fid. I guess if it isn't it probably should be. :)
Non-standard form element properties.
#progress_indicatoroptions are 'none', 'bar', and 'throbber', default is 'throbber'.#progress_message(string) Progress message to display along with progress meter while a file is being uploaded. Defaults to NULL.#upload_validators(array) an array of callback functions to perform validation of uploaded files.#upload_location(string) location on server where uploaded files should be stored. e.g.) 'public://files/my_files_director'The image_example module provides a good example of basic usage in the image_example.pages.inc file.
Hope that helps, let me know if you have any questions or if any of that is unclear.
Comment #5
rfayI added this to D7 FAPI. Oh my goodness it's painful to add an element type.
Review is appreciated, http://api.drupal.org/api/drupal/developer--topics--forms_api_reference....
Comment #6
jhodgdonLooks pretty good Randy...
A couple of issues in the section http://api.drupal.org/api/drupal/developer--topics--forms_api_reference....
a) Missing the Properties: #access, etc. line.
b) " Something like the following within your submit handler should do the trick." -- missing the "following" code?
Comment #7
jgraham commentedAttached patch addresses the issues in comment 6 as well as providing an inline example like elsewhere in the doc
Note: New files are uploaded with a status of 0 and are treated as temporary files which are removed after 6 hours via cron. Your module is responsible for changing the $file objects status to FILE_STATUS_PERMANENT and saving the new status to the database. Something like the following within your submit handler should do the trick.
Properties: #access, #after_build, #array_parents, #attached, #attributes, #description, #disabled, #element_validate, #parents, #post_render, #prefix, #pre_render,#process, #states, #suffix, #theme, #theme_wrappers, #title, #tree, #weight
Usage example (image_example.pages.inc.):
Comment #8
jgraham commentedComment #10
rfayLooks good to me. I sure appreciate you picking this up.
Comment #11
jgraham commentedcommitted to head
Comment #12
rfayThanks so much, jgraham!
Comment #14
aaronelborg commentedAlright.........what am I missing here.
eojthebrave said:
How does a validate and/or a submit handler get called when the upload button triggers an ajax call?
Thanks for any advice.
Comment #15
aaronelborg commentedHuh. Nothing?
Well here's what I ended up doing.
THEN.....my custom submit handler gets called and all is right within the world. (Except for the fact that this feels wicked-hacky.)