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 .

Xin

3 thoughts on “drupal views 2 theming: access other fields value in your field template

  1. Hi – thanks for the post. I used the loop you described to get my FIELD_ID values, but when I try

    echo $row->{$view->field[‘field_product_short_desc’]->field_alias};

    it just returns “1”, same with all the other fields.

    Do you know what might be up? Using D7 and views 3…

Leave a Reply

Your email address will not be published. Required fields are marked *