3

I have a view set up that uses an image from a node, I have customized the views-view-field.tpl to change the url the image links to dependent on the content type. I am trying to print out the picture using field_view_field in the .tpl, but its just printing out "array". Here is what I'm working with.

<?php if ($row->node_type =='article'): ?>
<a href="http://www.someurl.com"><?php print field_view_field('node', $row, 'field_field_image');?></a>

What am I doing wrong? Thanks.

3

Ok, so I figured it out. First I did a node load:

<?php $node = node_load($row->nid);?>

Then I had to wrap my field_view_field in a drupal_render, finally I added my image style name in the display parameter, like this:

<?php print drupal_render(field_view_field('node', $node, 'field_image', 'image_style'));?> 
1

Print out the field using print_r(). You will probably find that there are safe and raw values at the very least and it will be most likely the safe value you want.

EDIT:

In the comments oobie11 points to Rendering Drupal 7 fields (the right way). Bookmark this :)

Looking at the documentation for field_view_field tells me that the second argument is the actual entity. I don't think for this purpose the $row variable counts as an entity. You will need to do a node_load first getting the nid from the $row and pass the returned node to field_view_field.

  • Wont that restrict me to one language? – oobie11 Dec 22 '11 at 8:54
  • ah, you can use something like $user->language to access the correct language. You're right, it will be a language component to the array. – Malks Dec 22 '11 at 8:56
  • I'll see if I can find an example, I'm sure I've done it in the past. – Malks Dec 22 '11 at 8:57
  • I read here (computerminds.co.uk/articles/…), that field_view_field was the best way to do this. Not sure if the are correct, but it sounded good. – oobie11 Dec 22 '11 at 9:02
  • Ah I think I see what's wrong here. I'll edit the original answer. – Malks Dec 22 '11 at 22:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.