0

When creating a view, I rewrote the output in "Rewrite Results". There I have an HTML with a link to an icon in the theme folder.

<ul class="stats vertical">
       <li><a href="#"><img src="sites/all/themes/my_theme/images/my_icon.png">[field_output]</a></li>
       <li><a href="#"><img src="sites/all/themes/my_theme/images/my_icon.png_orange.png">[field_text_output]</a></li>
</ul>

As you can see that I am hard-coding the path to my icons. This works if this View Page is accessed from home page. But when this View page is accessed from some 2nd or 3rd level page, the icons are not shown.

Is there any way I can get a relative path to my theme within Views?

Thanks.

2 Answers 2

1

I will encourage you to rethink your hardcoding approach and instead implement your icons as CSS background images, using views to apply the desired classes to your field elements. This approach will allow you to avoid the options I am about to suggest :)

Option 1: Copy one of the following files found in the views module theme folder into your theme's templates folder: views-view-field.tpl.php, views-view-fields.tpl.php and rename it according to your needs using the Views template files documentation - and in your case you may want to look at views-view-list.tpl.php. Then code your img tag into this field template logic, constructing your img src with PHP using the $base_url variable. Clear the cache.

Option 2: Install Views PHP and add a Global:PHP field and construct your img src with PHP using the $base_url variable.

3
  • You. are. awesome!
    – user219492
    May 28, 2014 at 20:49
  • Awe shucks, thank you. Please remark on which part of my answer ended up being the ticket for your needs.
    – LunkRat
    May 28, 2014 at 20:53
  • I Installed Views PHP and added a Global:PHP field by using the $base_url variable. :-)
    – user219492
    May 29, 2014 at 1:24
1

Yes we can make no need to go for tpl and all

 function MODULE_init(){
    /*define the theme path for Image and JS folder*/
    $ThemeImgPath = drupal_get_path('theme', 'themename');//for getting the theme path
    if($ThemeImgPath) {
             define("THEMEIMAGEPATH", base_path().$ThemeImgPath . "/images/"); //for getting the path to images folder in the theme 
             define("THEMEJSPATH", base_path().$ThemeImgPath . "/js/");//to get the js folder path in the themes
               define("THEMEPATH", base_path().$ThemeImgPath);
         }
    }

define this in any of the custom module init() and use it any where in your project in views, themes, modules etc.

use THEMEIMAGEPATH - to get the image path in themes,

THEMEJSPATH - to get the js path in themes,

THEMEPATH to ge the theme path.

2
  • How to use this on Views after adding this hook function? I added this to the custom module and the "THEMEPATH" does nothing on <img src="THEMEPATH/images/testing.jpg" />
    – CocoSkin
    May 16, 2018 at 13:18
  • how to render the path in views?
    – CocoSkin
    May 29, 2018 at 13:46

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.