I have a content-type that has an imagefile and a url; I want the url to wrap around the image. So I'm trying to make a viewtemplate based on the information in Theming information

based on this description

  * This template is used to print a single field in a view. It is not
  * actually used in default Views, as this is registered as a theme
  * function which has better performance. For single overrides, the
  * template is perfectly okay.
  * Variables available:
  * - $view: The view object
  * - $field: The field handler object that can process the input
  * - $row: The raw SQL result that can be used
  * - $output: The processed output that will normally be used.
  * When fetching output from the $row, this construct should be used:
  * $data = $row->{$field->field_alias}
  * The above will guarantee that you'll always get the correct data,
  * regardless of any changes in the aliasing that might happen if
  * the view is modified.

I made views-view-field--my-view--block--field-image.tpl.php to look like this:

$url = $row->{$field->URL}
<a href="<?php echo $url; ?>" target="_blank">
  <?php print $output; ?>

But I'm getting Cannot access empty property on the $row->{$field->URL} line. Can I only access the image field from within the template for the image field? I was looking at views-view-fields--my-view--block.tpl.php but not sure how to access things from in there

  • 1
    When dealing with a filed template I have never been able to retrieve data for any other field besides the one for which I am creating the template. You might want to consider creating a template for the row, or the style of the view. Then you can have access to all fields available to $row. Nevertheless I wanted to say that you can create this functionality without a template. Add a global text field to your view and use the link and image tokens to create the link.
    – Perisdr
    May 15, 2012 at 15:34
  • @Perisdr I thought it should be possible!... could you explain more about how to do that? not sure what links and image tokens are
    – Damon
    May 15, 2012 at 15:39
  • I think @Perisdr is correct that it would be easier to do this at the row level vs field, or todo a rewrite also a great idea... :) May 15, 2012 at 15:54
  • @Damon when you select text rewrite option you will see suggested tokens below it. May 15, 2012 at 15:56
  • i dont' see a text-rewrite option.. do I need the tokens module for what you're talking about to be an option?
    – Damon
    May 15, 2012 at 16:55

2 Answers 2


I would do the following: <pre> <?php print_r ($row); ?> </pre> This will give you the full tree of the $row

  • Good thinking. I isolated it down to: $row->_field_data[nid][entity]->field_url[und][0][safe_value]); Is that safe to use as the value of the field?
    – Damon
    May 15, 2012 at 15:38
  • try $row['field_url']['und']['0']['safe_value']; May 15, 2012 at 15:46
  • sorry, missed your edit... thats why its called safe_value :) May 15, 2012 at 15:51
  • heh yeah.. I was just concerned that the string might not always be the same, though I don't see why it wouldn't
    – Damon
    May 15, 2012 at 17:20

Instead of doing this in a template file you could do this by using Rewrite Results within the field settings of a view, either by creating your own Global custom text field or editing the settings of the link field or image field. In the rewrite results section you can create a link around the Image as long as the image and the link are both fields within the view.

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.