0

Considering that I know the NID of the node, how can I get the value of one of the field that belongs to that NID?

I tried everything I can to get the value of field_code_file with the following code:

$node = node_load($_GET['nid']);
$wrapper = entity_metadata_wrapper('node', $node);
$file = $wrapper->field_code_file->value();

The value returned by $file is "array". Here's the value:

Array ( [#theme] => field [#weight] => 0 [#title] => Code File [#access] => 1 [#label_display] => above [#view_mode] => _custom_display [#language] => und [#field_name] => field_code_file [#field_type] => file [#field_translatable] => 0 [#entity_type] => node [#bundle] => source_code [#object] => stdClass Object ( [vid] => 71 [uid] => 1 [title] => Billing Software [log] => You can now download the installer for Billing System. This is important if you don't have crystal reports v8.5 in your machine. [status] => 1 [comment] => 2 [promote] => 1 [sticky] => 0 [ds_switch] => [nid] => 29 [type] => source_code [language] => und [created] => 1227189989 [changed] => 1260934297 [tnid] => 0 [translate] => 0 [revision_timestamp] => 1260934297 [revision_uid] => 1 [body] => Array ( [und] => Array ( [0] => Array ( [value] =>

) ) ) [field_screenshot] => Array ( [und] => Array ( [0] => Array ( [fid] => 36 [alt] => [title] => [width] => [height] => [uid] => 1 [filename] => Billing Screenshot.jpg [uri] => public://images/admin/Billing Screenshot.jpg [filemime] => image/jpeg [filesize] => 35357 [status] => 1 [timestamp] => 1227589659 ) ) ) [field_code_file] => Array ( [und] => Array ( [0] => Array ( [fid] => 37 [display] => 1 [description] => [uid] => 1 [filename] => Billing System.zip [uri] => public://download/admin/Billing System.zip [filemime] => application/zip [filesize] => 864517 [status] => 1 [timestamp] => 1227589659 ) ) ) [field_terms_and_conditions] => Array ( [und] => Array ( [0] => Array ( [value] => 1 ) ) ) [field_language] => Array ( [und] => Array ( [0] => Array ( [tid] => 7 [taxonomy_term] => stdClass Object ( [tid] => 7 [vid] => 5 [name] => Visual Basic [description] => [format] => [weight] => 0 [vocabulary_machine_name] => vocabulary_5 [metatags] => Array ( ) ) ) ) ) [field_tags] => Array ( [und] => Array ( [0] => Array ( [tid] => 52 [taxonomy_term] => stdClass Object ( [tid] => 52 [vid] => 10 [name] => visual basic [description] => [format] => [weight] => 0 [vocabulary_machine_name] => vocabulary_10 [metatags] => Array ( ) ) ) [1] => Array ( [tid] => 55 [taxonomy_term] => stdClass Object ( [tid] => 55 [vid] => 10 [name] => billing [description] => [format] => [weight] => 0 [vocabulary_machine_name] => vocabulary_10 [metatags] => Array ( ) ) ) [2] => Array ( [tid] => 323 [taxonomy_term] => stdClass Object ( [tid] => 323 [vid] => 10 [name] => cable tv [description] => [format] => [weight] => 0 [vocabulary_machine_name] => vocabulary_10 [metatags] => Array ( ) ) ) ) ) [metatags] => Array ( ) [cid] => 75722 [last_comment_timestamp] => 1386138557 [last_comment_name] => Legendx [last_comment_uid] => 0 [comment_count] => 442 [name] => admin [picture] => 1 [data] => a:7:{s:14:"picture_delete";i:0;s:14:"picture_upload";s:0:"";s:7:"contact";i:1;s:13:"form_build_id";s:37:"form-df16b3b26332f9a4dd7de12c3f483943";s:9:"nodewords";a:9:{s:8:"abstract";a:1:{s:5:"value";s:0:"";}s:9:"canonical";a:1:{s:5:"value";s:0:"";}s:9:"copyright";a:1:{s:5:"value";s:0:"";}s:11:"description";a:1:{s:5:"value";s:0:"";}s:8:"keywords";a:1:{s:5:"value";s:0:"";}s:13:"revisit-after";a:1:{s:5:"value";s:1:"1";}s:6:"robots";a:2:{s:5:"value";a:6:{s:9:"noarchive";i:0;s:8:"nofollow";i:0;s:7:"noindex";i:0;s:5:"noodp";i:0;s:9:"nosnippet";i:0;s:6:"noydir";i:0;}s:11:"use_default";i:0;}s:8:"dc.title";a:1:{s:5:"value";s:0:"";}s:8:"location";a:2:{s:8:"latitude";s:0:"";s:9:"longitude";s:0:"";}}s:4:"info";s:106:"To receive an update on your email please enter your email at Feed subscription at the right of this page.";s:6:"format";s:1:"2";} [_field_view_prepared] => 1 [entity_view_prepared] => 1 [content] => Array ( ) ) [#items] => Array ( [0] => Array ( [fid] => 37 [display] => 1 [description] => [uid] => 1 [filename] => Billing System.zip [uri] => public://download/admin/Billing System.zip [filemime] => application/zip [filesize] => 864517 [status] => 1 [timestamp] => 1227589659 ) ) [#formatter] => file_default [0] => Array ( [#theme] => file_link [#file] => stdClass Object ( [fid] => 37 [display] => 1 [description] => [uid] => 1 [filename] => Billing System.zip [uri] => public://download/admin/Billing System.zip [filemime] => application/zip [filesize] => 864517 [status] => 1 [timestamp] => 1227589659 ) ) )

I am expecting a value like this:

http://www.mysite.com/sites/default/files/download/admin/Billing%20System.zip

I'm using this code in my simple module that I created.

For Jeet:

Thanks. When I use this command print_r($file), it returns this value:

Array ( [0] => Array ( [fid] => 37 [display] => 1 [description] => [uid] => 1 [filename] => Billing System.zip [uri] => public://download/admin/Billing System.zip [filemime] => application/zip [filesize] => 864517 [status] => 1 [timestamp] => 1227589659 ) )

Now how can I get the full url like:

http://www.mysite.com/sites/default/files/download/admin/Billing%20System.zip

I'm trying to understand what you mean by file_create_url($uri), but I can't crack my head on it.

Can you please explain it to me further?

Thank you

2
  • Since you haven't shown what is failing, eg, what is being returned instead of what you are hoping for, perhaps deeson-online.co.uk/labs/… can shed some light on the various ways of doing this
    – Jimajamma
    Commented Jan 6, 2014 at 5:13
  • Hi, I updated my post.
    – jaypabs
    Commented Jan 6, 2014 at 5:42

1 Answer 1

3

Assuming the field is a file type.

$node = node_load($_GET['nid']);
$file = field_get_items('node', $node, 'field_code_file');

Print the $file. you will get a URI index. To get your full path to file use function file_create_url($uri)

Let me know if this doesn't get you the result.

2
  • Thank you. I almost there. I updated my post. Please try to look at it.
    – jaypabs
    Commented Jan 6, 2014 at 6:09
  • Glad I could help. :)
    – Jeet
    Commented Jan 6, 2014 at 7:22

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.