2

I have a content type that contains a field called headshot. This is a file field. In my custom node template file, I need to access the value of the headshot's filename property. dpr($content) produces this for the field:

[field_headshot] => Array
                        (
                            [und] => Array
                                (
                                    [0] => Array
                                        (
                                            [fid] => 53
                                            [display] => 1
                                            [description] => 
                                            [uid] => 10
                                            [filename] => kanderson.jpg
                                            [uri] => public://headshot/kanderson.jpg
                                            [filemime] => image/jpeg
                                            [filesize] => 21669
                                            [status] => 1
                                            [timestamp] => 1337356962
                                            [type] => image
                                            [rdf_mapping] => Array
                                                (
                                                )

                                            [image_dimensions] => Array
                                                (
                                                    [width] => 180
                                                    [height] => 252
                                                )

                                        )

                                )

                        )

I've tried

<img class="speaker_headshot" src="sites/default/files/styles/large/public/headshot/<?php print render($content['field_headshot'][0]['filename']); ?>" alt="<?php print $title; ?>" />

and

<img class="speaker_headshot" src="sites/default/files/styles/large/public/headshot/<?php print render($content['field_headshot']['filename']); ?>" alt="<?php print $title; ?>" />

with no success. How do I access this property?

1 Answer 1

2

To get a web-accessible URL for the file you need to use file_create_url() on the field's uri property. You can also make use of theme_image() so you don't have to write the HTML yourself:

$vars = array(
  'path' => file_create_url($content['field_headshot'][LANGUAGE_NONE][0]['uri']),
  'alt' => $title,
  'attributes' => array(
    'class' => array('speaker_headshot')
  )
);

$img = theme('image', $vars);

print $img;

Since this code will clutter up your template file you might want to consider putting in a preprocess function instead, something like:

function MYTHEME_preprocess_node(&$vars) {
  if ($vars['node']->type == 'headshot') {
    $items = field_get_items('node', $vars['node'], 'field_headshot');
    $image = array_shift($items);

    $image_vars = array(
      'path' => file_create_url($image['uri']),
      'alt' => $vars['title'],
      'attributes' => array(
        'class' => array('speaker_headshot')
      )
    );

    $vars['image'] = theme('image', $image_vars);
  }
}

Then in your template file you can just print $image; to get the HTML output.

12
  • Thank you, but that's more than I need to know! I just need to know how to access the VALUE of the filename attribute, and $content['field_headshot'][LANGUAGE_NONE][0]['filename'] doesn't work either.
    – EmmyS
    Commented May 21, 2012 at 18:37
  • But just so that I could say I tried, I added your preprocess function to my template.php, and ended up with this error: Fatal error: Cannot access empty property in /home/www/expo/modules/field/field.module on line 958
    – EmmyS
    Commented May 21, 2012 at 18:43
  • Ah, sorry about that I get carried away :) $content['field_headshot'][LANGUAGE_NONE][0]['filename'] is definitely the right variable if your output from dpr($content) is right above
    – Clive
    Commented May 21, 2012 at 18:43
  • I took a guess at your content type being called 'headshot' (it probably isn't), other than that I can't think of a good reason why you'd be getting that error
    – Clive
    Commented May 21, 2012 at 18:46
  • The output is correct; it's pasted directly from the node as rendered. But the variable you provide is NOT returning a value.
    – EmmyS
    Commented May 21, 2012 at 18:47

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.