3
votes

In Drupal 7.x, given a nid and a file field name, I'm trying to get the files (as $fid or objects) which have been uploaded as file attachments to that node.

Drupal will populate the uploaded files only when they are set as "displayed" but I need to do this for all the files of that field, no matters how they display setting is.

I've spent quite some time investigating into this but the new d7 file api looks pretty obscure from a documentation and code point of view. IMHO, too much "drupal magic" happening there for a fast understanding.

Any pointer to good documentation and/or suggestion on the solution greatly appreciated.

Thanks.

1
If you're doing this in your node.tpl.php file, then you should already have access to the current node. You should then be able to do something like $content['field_attached_file']['#items']['0']['value']; to access the file. See the node.tpl.php documentation for more info. - nmc
@nmc nope. I'm not doing this in node.tpl.php .. it's part of a module code. Moreover, I don't think it would be populated if the file has display set as disabled (didn't verify). Thanks for commenting anyway. - Fabio Varesano
If you want to check whether or not you have access to the file from where you are calling the code, try using Devel module to print out (dpm($node)) the node to see what you have access to. - nmc
@nmc been there, done that. The uploaded file informations are only available if the file has been set as "displayed" otherwise it won't be loaded. That's the point of my question, getting all the files uploaded, no matter of the display setting. But this looks very painful. - Fabio Varesano
@Fabio: Did you ever sort this out? - Egil Hansen

1 Answers

0
votes

Although your question seems a little unclear to me, but according to me:

you want to get all the files attached to a node. Just do $node = node_load($nid); and then check your $node->field_name['LANGUAGE_NONE'/lang code]

This will give you an array of files uploaded using that filefield.

Thanks