Hi there. I'd like to programmatically create an upload File field (with the type "managed_file") that takes advantage of all of the "File" module's built-in widget functionality. Specifically, I'm thinking of what happens when you create a File field using the Drupal GUI and set the "Number of values" to "Unlimited." By default, once a person uploads a document, a "File information" HTML table automatically appears above the field with a link to the file, plus a "Remove" button. Subsequent uploads also appear in the table and users can then drag them up or down to reorder them.

Activating that functionality via the GUI is one thing; but I'm having a difficult time doing so programmatically. I can easily code a "managed_file" field using the form API that can upload one file. However, I'd like to keep uploading additional files, where the "File information" table appears and users can reorder them.

I don't want to change any of the managed_file functionality, I just want to take advantage of what's already there. I've scoured the Internet for examples; but only found code for single-upload fields. Any recommendations/example code would be greatly appreciated.

Thanks!

Comments

Jaypan’s picture

There is no way to leverage the file module's functionality, as it is directly built into the Field API as a widget, and the Field API itself is what creates the multi-value option, along with the ajax that handles it, and the tabledrag that allows you to reorder items.

If you want to re-create this as a Form API element, you'll have to create your own #ajax structure to handle adding/removing new items, and you'll have to implement the Tabledrag API in order to re-order the items. You'll also have to create your own database tables using the Schema API to hold the values, as well as the weights (order) of the elements.

It's not a scenario for the light of heart.

slanger’s picture

I really appreciate the information, Jaypan. It's too bad that I can't take advantage of the file module's widget functionality programmatically (via the Form API) like I was hoping. However, it's better to know that now, rather than after spending weeks beating my head against a wall, fighting a losing battle.

Thanks again!