2

UPDATE: Thanks to Molot I now see how I can edit the output of the view using the specific tpl file for that field. However I'm still not sure how to refer to that field and print it within that tpl file.


I have a file field in a custom content type that I'm displaying in a view. I want it to spit out the path to the file and nothing else in the view so I can do whatever I want with it. I have customized the views-view-unformatted-nameofview.tpl.php so it doesn't wrap the entire view with the default drupal divs and classes, like so:

<?php foreach ($rows as $id => $row):
    print $row;
endforeach; ?>

Which works but is there a way to customize how it spits out a specific field? Even though I customize the html style to none it still tries to do things like display a custom icon for my file which I don't want. I'm also rewriting the output of the field in the view like so

<img src="[field_front_page_icon]">

I want to embed it into an img tag but what I end up with is

<img src="&lt;span class=" file"="">    
<img class="file-icon" alt="" title="image/svg+xml"  src="/modules/file/icons/image-x-generic.png"> 
<a  href="https://url.com/sites/default/files/maps.svg" type="image/svg+xml;  length=5657"> 
    maps.svg 
</a>

When all I want it to display is

<img src="https://url.com/sites/default/files/maps.svg">

I would normally use an image field but it doesn't seem to allow to add svg as an acceptable file type. I'm open to other ways to go about it, but even if that's the case it'd still be nice to know how to edit the field output in views. I'd rather not install a pre-existing module if at all possible. However I would understand if that is the only reasonable course of action.

  • Why rewrites instead of tpl files? And why don't you use example.com as substitution domain? That's what it is for. I'm pretty sure url.com is not Drupal-based. – Mołot May 28 '15 at 13:46
  • I'd be totally fine rewriting using a tpl file. I'm just not sure what tpl file would be required to edit the output of the field specifically in that view. And I'm not exactly sure how I'd edit the output, and how to refer to that field within the tpl. – Matt May 28 '15 at 13:47
  • See here (disclaimer: that's not my image, it just shows what I wanted to tell). I don't have time to write full answer, sorry. – Mołot May 28 '15 at 13:49
  • Great! I see how I can edit the output for that specific field now. But I'm not sure how to refer to that field within it. – Matt May 28 '15 at 13:56
  • if you will click on a filename in the window opened by link I shown you, you will see well-commented default. Play with it. That's all I can tell now, gotta get back to Excel :( Or you can wait for someone else with time. But experimenting on your own will not stop people from answering here, so... – Mołot May 28 '15 at 13:59
2

If you just want to rewrite the file field in Views to something like

<img src="https://url.com/sites/default/files/maps.svg">

All you have to do is

  1. add that File Field in Fields section
  2. Inside the File Field configure, select "URL to file" on Formatter
  3. Inside the File Field configure rewrite results section, check the "Rewrite the output of this field" and rewrite something like "<img src="[field_file] ">" on Text area. Then click Apply.

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.