1

I have a field for uploading a file. And in *node--content_type.tpl.php* I want to just print the filename of the uploaded file.

So far I have this

$node = node_load($nid);
//get field item in correct language
$item = field_get_items('node', $node, 'field_file_upload');
//get field_file_upload value
$output1 = field_view_value('node', $node, 'field_file_upload', $item[0]);
print render($output1);

But it renders a link to the download and a thumbnail of the download type.

I want to just print out the filename. I tried using:

$output2 = field_view_value('node', $node, 'field_file_upload', $item[0]['filename']);
print render($output2);

But this renders a thumbnail of the download type e.g PDF icon rather than showing the filename.

The printed array for $output1 is:

Array
(
    [#theme] => file_link
    [#file] => stdClass Object
        (
            [fid] => 196
            [display] => 1
            [description] => 
            [uid] => 1
            [filename] => drupal-7-theming-cheat-sheet.pdf
            [uri] => public://drupal-7-theming-cheat-sheet_0.pdf
            [filemime] => application/pdf
            [filesize] => 224804
            [status] => 1
            [timestamp] => 1348746651
            [type] => undefined
            [rdf_mapping] => Array
                (
                )

        )

    [#access] => 1
)

The printed array for $output2 is:

Array
(
    [#theme] => file_link
    [#file] => stdClass Object
        (
            [scalar] => drupal-7-theming-cheat-sheet.pdf
        )

    [#access] => 1
)

Any ideas? Thanks for your help!

1 Answer 1

2

I think you missed a key in between. Try,

$output2 = field_view_value('node', $node, 'field_file_upload', $item[0]);
foreach ($output2 as $key) {
  if(is_object($key)) {
    echo "File Name: ".$key -> filename;
  }
}
6
  • Hey @indrock it throws up an error when I try to print this Notice: Undefined index: #file in include() Sep 27, 2012 at 13:55
  • 1
    ahh, yes, thats because in array i can see that #file is an stdClass object and not a key. So, you should access it using -> and not like general array keys. See the edit
    – AjitS
    Sep 27, 2012 at 14:02
  • $output2 = field_view_value('node', $node, 'field_file_upload', $item[0][]['filename']); completely breaks drupal and stops it rendering anything else of the page :S Sep 27, 2012 at 15:01
  • Sorry, my mistake, see the edit. It should work.
    – AjitS
    Sep 27, 2012 at 15:03
  • 1
    I'm not sure, but the $output2 is an array with 3 elements, one of which #file is an object, which can't be accessed unless we know its name. And I'm not sure of the name of the object. above code won't cause any overhead, as it only checks for objects in $output2 array.
    – AjitS
    Sep 27, 2012 at 15:16

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.