2

I have a view that is showing a single image, and I need the image to link to a specific URL, but when I try using Rewrite Results on the field it will still show the image but doesn't add the link.

I have tried both "Rewrite the output of this field" putting in <a href="http://example.com">[field_file]</a> and "Output this field as a link" putting http://example.com in the field and neither of those ways worked.

How can I rewrite the field to have the link on the image?

2
  • 1
    'Output this field as a link' should do the trick. What is HTML output of the field if this is enabled?
    – Wtower
    Commented Dec 18, 2014 at 15:41
  • @Wtower <div class="content"><img typeof="foaf:Image" src="example.com/sites/default/files/logo.gif></div> is all it does, but chadpeppers solution worked for me
    – diego
    Commented Dec 18, 2014 at 15:45

2 Answers 2

4

You can use the image url formatter module to output the path to the image. Then do like you tried before to Rewrite Results and add the markup.

<a href="http://www.example.com"><img src="[field_file]" /></a>
2
  • 1
    This is nice, I am curious to why a simple rewrite doesn't help.
    – Wtower
    Commented Dec 18, 2014 at 15:47
  • 1
    Like @Wtower, I'm also interested to know why Views 'Output this field as a link' doesn't solve your problem. I assume you have cleared the cache when changing the View, or did that happen when you installed the Image URL Formatter module?
    – Watergate
    Commented Dec 18, 2014 at 17:11
0
  1. global $base_path;

get the base path (if you globally declared in settings.php)

  1. $profile_pic= !empty($data->_field_data['uid']['entity']->field_profile_pic) ? $data->_field_data['uid']['entity']->field_profile_pic : "";

Get the image field url from $data.

  1. $Logo = file_create_url($profile_pic['und'][0]['uri']);

Get the URI of that image value if the image is present store that URI

$link_path = "path to where the link has to go on clicking the image"; print the image under a link

<a href="<?php print $link_path;?>"><img alt="profile picture" src='<?php print $Logo.'?'.time();?>' width="126" height= "173"></a>

here the time is to avoid cache problem with image in drupal it will retrive the latest image from the server.

hope this helps

1
  • It would help if you would give your code a context, eg a view-...tpl.php perhaps?
    – Wtower
    Commented Dec 19, 2014 at 8:12

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.