8

How do you set an existing file path for a file field, in Drupal 7? The only option I see is to upload the file again.

In Drupal 6, FileField Sources seems to do this.

Does FileField Paths or IMCE along with file field allow assignment of server files?
Is there an API to programmatically manage file fields?

Edit
I just discovered that this can all be done with the media module. I also have mediaelement installed. However it is not completely intuitive, so here are the steps I took:

Import to media library: /admin/content/media/import

Create a mediafile asset field. This allows you to attach from the library.

Display formats (example for audio) are at /admin/config/media/types/manage/audio/display/, instead of content type "manage display" which only provides "large", "small", "preview", etc.

6 Answers 6

3

I would try Media and its associated modules. It's an evolution of FileField Sources, and provides most of the same kind of functionality although with Drupal 7 fields.

0
3
+50

I can help you with the attaching a file to a filefiled. However, I have not attempted to achieve an autocomplete widget to get the file that you want to attach to the filefield.

you can use file_get_contents() to grab the file that you want to attach and then use file_save_data to get it into Drupal. The catch is that you'll need to change the status of the file you just added so that you can attach it to a node later. Other wise you'll get a validation error.

$file = file_save_data($data, $destination = NULL, $replace = FILE_EXISTS_RENAME);

db_update('file_managed')
  ->fields(array('status' => 0))
  ->condition('fid', $file->fid, '=')
  ->execute();

Once the form is submitted with the fid as the value for the filefield that you want file attached to the managed files status will be updated automatically.

Check out http://api.drupal.org/api/drupal/includes--file.inc/group/file/7 for further reference.

3
  • 1
    This looks very promising. Two quick questions: 1) To attach to a node afterwards, $node = node_load($nid); $node->field_myfilefield[$node->language][0] = $file; node_save($node); should work? 2) I assume I should set the status back to 1 after attaching to the node? Or does node_save do this for me? Thanks!
    – brian_d
    Commented Apr 5, 2011 at 22:47
  • Your assumptions are correct about saving the node. However, doing so sets the status back to 1 automatically so you don't have to worry about manually doing it. I struggled with this one for a while as there was not a function to add a file to the managed files table without setting the status to 1. I've used it in a batch operation that downloads files from another server and attaches them to nodes on my Drupal site. On a side not if you are using a batch then you can't use file_get_contents() it will fail. You should use drupal_http_request().
    – Jepedo
    Commented Apr 5, 2011 at 23:40
  • On second thought I'm not sure if node_save changes the status to 1. In the example I was referring to I used drupal_form_submit() to ensure that the node_save ran through validation. In that case the status was updated to 1 automatically. You might have to update it manually in your case. Chances are that node_save() will behave the same though.
    – Jepedo
    Commented Apr 6, 2011 at 0:54
3

I chose IMCE in combination with IMCE for FileField (D7) because the client needed a quick and unbloating selection of files uploaded with ftp - and this without duplicating them for each node.

As far as i see the Filefield Sources always duplicates attached files at the moment. The importer provided by Media does that too.

3

FileField Sources has been ported for D7 and it is great. Too there is module called Visual select file what adds abbiliti for nice Views-based select of existing files (good for images).

1

You could always go for the File as Node approach and use a Node Reference field + Custom Formatters (to render the Node as a File) to do this, which would allow you to update the File node and have all references of that File node updated across the board.

In D6 you can also use Node Relationships to provide a File Library and Formatters 4 Node Reference field to format the node as any available FileField formatter.

1

FileField Sources has not yet been ported to Drupal 7, but it's planned. See the corresponding issue on drupal.org.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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