If you look at the comment in the beginning of the template file you're overriding, you should notice that it lists the variables available for any override code.
For instance, in field.tpl.php
(used as starting point for overrding field templates), you should see the following line inside that comment:
* - $element['#object']: The entity to which the field is attached.
This gives you the name of the global ($element['#object']
) to examine to look for the values of the fields.
Now, you need to look "inside" this object to figure out how to access the values of these fields. To do so, the Devel module is what you need, so install and enable this for your (test) site.
Then put the following near the top of your template override file:
<?php dpm($element['#object']); ?>
When you look at the page again as administrator (clear cache and reload if necessary), there should be a Krumo accordion in the message area that expands this object. Expanding it will tell you how to get to the value of each field present in the entity. For instance, it may tell you that the value of field_one
is $element['#object']->field_one['und'][0]['value']
.
Now, you should be able to extract and add the fields, e.g.:
<?php
$field_one = $element['#object']->field_one['und'][0]['value'];
$field_two = $element['#object']->field_two['und'][0]['value'];
$sum = $field_one + $field_two;
?>
Disable Devel and delete the call to dpm()
on a production site.