I have a view that I want to create a view template for in my theme.

So, I have created: views-view--homepage-suppliers.tpl.php

I have then several content items of content type: Supplier that the view uses.

The have various fileds, such as title, field_supplier_image

How do i then access these fields in this view template?

Is it something along the lines of:



3 Answers 3


It would appear your using a 'Display output' template when you probably want a 'Row style' output so you can access fields. You will need to change your tempate by going to 'advanced > theme information', it will probably be something similar to 'views-view-fields--homepage-suppliers.tpl.php'.

You can then get a nid from the field and access a whole host of information through the node object.

  $nid = $row->{$view->field['field_supplier_image']->field_alias}; 
  $node = node_load($nid);

If you var_dump() the $node variable you can see what you have to play with.

  • That's exactly what I was after!!
    – terrid25
    Commented May 30, 2012 at 11:22
  • Any chance of ticking this as answered then? ;-) Commented May 30, 2012 at 14:59

I ended up using this in a field template:


You can access fields through the $view->result object.

Here's an example I use to create a ical calendar:


global $base_url;

function ical_extract_value($field,$key='value'){
    return $field['und'][0][$key];

function ical_shorten_line($line){
    return trim(chunk_split($line,76,"\r\n "));

print 'PRODID:'.$base_url.'/'.current_path().CRLF;

foreach ($view->result as $node){
//      print_r($data); // uncomment to see available data
        print 'BEGIN:VEVENT'.CRLF;
        print 'UID:'.$data->nid.'@'.$base_url.CRLF;
        $description = ical_extract_value($data->body);
        print ical_shorten_line('DESCRIPTION:'.$description).CRLF;
        print 'DTSTART:'.date($ical_time_format,ical_extract_value($data->field_start)).CRLF;
        print 'DTEND:'.date($ical_time_format,ical_extract_value($data->field_start,'value2')).CRLF;
        /** other fields here **/
        $keywords = ical_extract_value($data->field_tag_list);
        $keywords = str_replace(' ', ',',$keywords);
        print ical_shorten_line('CATEGORIES:'.$keywords).CRLF;
        print 'END:VEVENT'.CRLF;
} ?>

As you can see, a certain field value can be accessed by $view->result[n]->filed_xxx['und'][0]['value'].

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.