I am trying to print the filefield description. I have tried several things and can't seem to get it.

I have a simple file upload content type. It's machine name is share_files. The only field is the filefield and I have the description turned on. It is named "field_sfile."

The best that I have come up with is print $content->field_sfile['description']['0']['value'];

I am not really a coder, but I am trying to learn. My ultimate goal is a small module that will use the description field to create the title. I want to use auto nodetitle to help with this, but there is no token for the filefield description.

Below is the relevant information from adding print_r(array_keys($content)); to the body of an overridden template file for this content type.

  ["field_sfile"]=>
  array(16) {
    ["#theme"]=>
    string(5) "field"
    ["#weight"]=>
    string(1) "1"
    ["#title"]=>
    string(12) "Shared Files"
    ["#access"]=>
    bool(true)
    ["#label_display"]=>
    string(5) "above"
    ["#view_mode"]=>
    string(4) "full"
    ["#language"]=>
    string(3) "und"
    ["#field_name"]=>
    string(11) "field_sfile"
    ["#field_type"]=>
    string(4) "file"
    ["#field_translatable"]=>
    string(1) "0"
    ["#entity_type"]=>
    string(4) "node"
    ["#bundle"]=>
    string(11) "share_files"
    ["#object"]=>
    object(stdClass)#88 (29) {
      ["vid"]=>
      string(2) "66"
      ["uid"]=>
      string(1) "1"
      ["title"]=>
      string(22) "[node:menu-link:title]"
      ["log"]=>
      string(0) ""
      ["status"]=>
      string(1) "1"
      ["comment"]=>
      string(1) "1"
      ["promote"]=>
      string(1) "1"
      ["sticky"]=>
      string(1) "0"
      ["nid"]=>
      string(2) "66"
      ["type"]=>
      string(11) "share_files"
      ["language"]=>
      string(3) "und"
      ["created"]=>
      string(10) "1316383429"
      ["changed"]=>
      string(10) "1316446540"
      ["tnid"]=>
      string(1) "0"
      ["translate"]=>
      string(1) "0"
      ["revision_timestamp"]=>
      string(10) "1316446540"
      ["revision_uid"]=>
      string(1) "1"
      ["field_sfile"]=>
      array(1) {
        ["und"]=>
        array(1) {
          [0]=>
          array(12) {
            ["fid"]=>
            string(2) "85"
            ["display"]=>
            string(1) "1"
            ["description"]=>
            string(28) "June 2011 New Blu-ray Movies"
            ["uid"]=>
            string(1) "1"
            ["filename"]=>
            string(18) "jun_11_blu-ray.pdf"
            ["uri"]=>
            string(40) "public://shared_files/jun_11_blu-ray.pdf"
            ["filemime"]=>
            string(15) "application/pdf"
            ["filesize"]=>
            string(6) "190898"
            ["status"]=>
            string(1) "1"
            ["timestamp"]=>
            string(10) "1316446540"
            ["origname"]=>
            string(18) "jun_11_blu-ray.pdf"
            ["rdf_mapping"]=>
            array(0) {
            }

Comments

mikeybusiness’s picture

This prints the link to the file upload with the file name as added by the description and the label.
print render($content['field_sfile']);

All I want is the description. I thought I could write something like below.
print render($content['field_sfile']['description']);
It just does not work.

mikeybusiness’s picture

Solved

I figured out some of what I was doing wrong and elaborated on the original tries, but for some reason I could never figure out how to get down into the #object array which contained the first instance of the description that was trying to get. I would get an error that began with "Cannot use object of type stdClass as array." But if you scroll down to the *RECURSION notation, I was able to print render the description through the #item array with the following code.

print render($content['field_sfile']['#items'][0]['description']);

I am just learning some of this, so an experienced developer may be able to elaborate or correct any of my vocabulary.

Here is the print_r of ($content['field_sfile']); for reference to see how I tracked down the data that I was trying to print out. I hope this helps someone.

Array
(
    [#theme] => field
    [#weight] => 1
    [#title] => Shared Files
    [#access] => 1
    [#label_display] => above
    [#view_mode] => full
    [#language] => und
    [#field_name] => field_sfile
    [#field_type] => file
    [#field_translatable] => 0
    [#entity_type] => node
    [#bundle] => share_files
    [#object] => stdClass Object
        (
            [vid] => 67
            [uid] => 1
            [title] => [node:menu-link:title]
            [log] => 
            [status] => 1
            [comment] => 1
            [promote] => 1
            [sticky] => 0
            [nid] => 67
            [type] => share_files
            [language] => und
            [created] => 1316548658
            [changed] => 1316548658
            [tnid] => 0
            [translate] => 0
            [revision_timestamp] => 1316548658
            [revision_uid] => 1
            [field_sfile] => Array
                (
                    [und] => Array
                        (
                            [0] => Array
                                (
                                    [fid] => 92
                                    [display] => 1
                                    [description] => Theatre 2011 Brochure
                                    [uid] => 1
                                    [filename] => tlt_brochure2011small.pdf
                                    [uri] => public://shared_files/tlt_brochure2011small_0.pdf
                                    [filemime] => application/pdf
                                    [filesize] => 1254462
                                    [status] => 1
                                    [timestamp] => 1316548658
                                    [origname] => tlt_brochure2011small.pdf
                                    [rdf_mapping] => Array
                                        (
                                        )

                                )

                        )

                )

            [rdf_mapping] => Array
                (
                    [rdftype] => Array
                        (
                            [0] => sioc:Item
                            [1] => foaf:Document
                        )

                    [title] => Array
                        (
                            [predicates] => Array
                                (
                                    [0] => dc:title
                                )

                        )

                    [created] => Array
                        (
                            [predicates] => Array
                                (
                                    [0] => dc:date
                                    [1] => dc:created
                                )

                            [datatype] => xsd:dateTime
                            [callback] => date_iso8601
                        )

                    [changed] => Array
                        (
                            [predicates] => Array
                                (
                                    [0] => dc:modified
                                )

                            [datatype] => xsd:dateTime
                            [callback] => date_iso8601
                        )

                    [body] => Array
                        (
                            [predicates] => Array
                                (
                                    [0] => content:encoded
                                )

                        )

                    [uid] => Array
                        (
                            [predicates] => Array
                                (
                                    [0] => sioc:has_creator
                                )

                            [type] => rel
                        )

                    [name] => Array
                        (
                            [predicates] => Array
                                (
                                    [0] => foaf:name
                                )

                        )

                    [comment_count] => Array
                        (
                            [predicates] => Array
                                (
                                    [0] => sioc:num_replies
                                )

                            [datatype] => xsd:integer
                        )

                    [last_activity] => Array
                        (
                            [predicates] => Array
                                (
                                    [0] => sioc:last_activity_date
                                )

                            [datatype] => xsd:dateTime
                            [callback] => date_iso8601
                        )

                )

            [cid] => 0
            [last_comment_timestamp] => 1316548658
            [last_comment_name] => 
            [last_comment_uid] => 1
            [comment_count] => 0
            [name] => hpladmin
            [picture] => 81
            [data] => a:1:{s:7:"overlay";i:1;}
            [uri] => Array
                (
                    [path] => node/67
                    [options] => Array
                        (
                            [entity_type] => node
                            [entity] => stdClass Object
 *RECURSION*
                        )

                )

            [entity_view_prepared] => 1
        )

    [#items] => Array
        (
            [0] => Array
                (
                    [fid] => 92
                    [display] => 1
                    [description] => Theatre 2011 Brochure
                    [uid] => 1
                    [filename] => tlt_brochure2011small.pdf
                    [uri] => public://shared_files/tlt_brochure2011small_0.pdf
                    [filemime] => application/pdf
                    [filesize] => 1254462
                    [status] => 1
                    [timestamp] => 1316548658
                    [origname] => tlt_brochure2011small.pdf
                    [rdf_mapping] => Array
                        (
                        )

                )

        )

    [#formatter] => file_default
    [0] => Array
        (
            [#theme] => file_link
            [#file] => stdClass Object
                (
                    [fid] => 92
                    [display] => 1
                    [description] => Theatre 2011 Brochure
                    [uid] => 1
                    [filename] => tlt_brochure2011small.pdf
                    [uri] => public://shared_files/tlt_brochure2011small_0.pdf
                    [filemime] => application/pdf
                    [filesize] => 1254462
                    [status] => 1
                    [timestamp] => 1316548658
                    [origname] => tlt_brochure2011small.pdf
                    [rdf_mapping] => Array
                        (
                        )

                )

        )

)