drupal views 2 theming: access other fields value in your field template

if you need access other fields in your field template, you can access them by

<?
echo $row->{$view->field[‘FIELD_ID’]->field_alias};
?>

You will be able to add some logic to your template like

<?

if($row->{$view->field[field_B_value]->field_alias})
{
echo $output  ;
}
else
{
echo “B is not available”;
}

?>

Note the ‘FIELD_ID’ can be something different depending on the field type. it can be “field_B_nid” (if it is node reference), or “field_B_value” if it is a regular text field, or “field_B_rating” (if a five star rating field). You can check all of your FIELD_ID by the following code

<?

foreach($view->field as $k=>$f){
echo $k;
}
exit;

?>

put the above code to your template file and view your view’s page url, you will see the output of the FIELD_ID of your fields.

the advantage of doing this instead of using directly $row->FIELD_ALIAS_NAME, is you don’t need update your views theme each time you update your view (for example, adding a new field), because the FIELD_ALIAS_NAME can change when fields are changed or reordered.

the credits go to vatavale at http://drupal.org/node/763620

good luck .

Read More