I'm trying to use a Views field template to insert PHP to convert a currency string to a number so that it can then be included in a math expression (can't do math on a string), as seen here https://www.drupal.org/docs/7/modules/views/theming-a-single-field-in-a-views-3-template

The field name is views_conditional and it outputs strings like "$2,560.00". I created a new template file named views-view-field--field-views-conditional.tpl.php with the following code in it and placed it in my theme's folder:

<?php print (int)preg_replace("/\..+$/i", "", preg_replace("/[^0-9\.]/i", "", $output)); 

?>

It is having no effect on the field output, so either the template is not being picked up or my code is wrong. Is there something I'm missing here?

Thanks in advance.

Comments

eriksandall’s picture

Have you cleared the cache?

hondaman900’s picture

Yes, indeed, cleared cache. Also turned off Views caching to eliminate caching issues/lag.

eriksandall’s picture

When you look at the view's theme information, is your custom .tpl the one that's in bold? If it's not bold, your file isn't being picked up for some reason. Otherwise, it could be your code. You could try a simple 'Hello World' as a sanity check.

hondaman900’s picture

Thanks Erik,

I had the correct field naming for the custom template file (tests like you suggested worked). However I found a solution by using the PHP number_format function with the following code:

<?php 

$showmoney = number_format($output, 0, '.', ',');
if ($showmoney != 0){
print "$$showmoney";
}

?>

Thanks for your attention and help with this.