0

My goal is to edit the output of a specific field in a view.

There is an image field, and I would like to specify how it is output. So that instead of

<img typeof="foaf:Image" src="https://url.com/sites/default/files/styles/com_home_news/public/field/image/myimage.jpg?itok=8e4_c1xI" width="290" height="140" alt="">

I can output something like

<img src="imagepath/image.jpg">

I'd like to be able to edit the output freely and just have access to the image path for this specific view.

I am currently editing

views-view-field--com-retheme-home-news--field-image.tpl.php

However I'm not sure how to actually access the path to the image. Using

<?php print $output; ?>

Only prints out what it would have before.

I understand that I could change the field itself to only present the path, however that isn't an option since there is already a lot of content taking advantage of how the field is already setup.

2

You need to install the Image URL Formatter module

Then in your view, click on your image field and select url as the formatter, so it prints the url rather than the image.

enter image description here

Option A) In the rewrite results field, use the token for your image field.

Example: <img src="[field_image]" class="yay-we-did-it">

Option B) if you want to use the .tpl.php file, put this inside it.

<img src='<?php print $fields["field_image"]->content; ?>' class='yay-we-did-it'>

  • This won't work in my scenario since I already have a large number of nodes that already have the image attached to the image field. My solution needs to make use of the content that is already present rather than adding more images to the new field on each previous node. My solution above grabs the URL as intended. – Matt Jun 23 '15 at 12:12
  • 1
    @Matt, you don't need to add more images. This module grabs the urls of the images. – No Sssweat Jun 23 '15 at 15:59
  • But I have other subdomains using different themes that need to use images without the image url. Wouldn't that break everything using the other theme? – Matt Jun 23 '15 at 16:02
  • 1
    nope, all this does is print/output the url rather than the image itself. In your view you get to choose weather you want the image or the url. – No Sssweat Jun 23 '15 at 16:05
0

It's a bit of a work around but I found a solution. What I ended up doing was use a regular expression on $output to grab the path and then used it to spit out what I wanted like so:

$pattern = '/src\s*=\s*"(.+?)"/';
$img = preg_match($pattern,$output,$results);
print "<img ".$results[0].">"; 

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.