1

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:

$rows['field_supplier_image']?

thanks

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 May 30 '12 at 11:22
  • Any chance of ticking this as answered then? ;-) – Jason Norris May 30 '12 at 14:59
0

I ended up using this in a field template:

$row->field_field_link_text[0]["rendered"]["#markup"]
0

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

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

(views-view--ical.tpl.php)

BEGIN:VCALENDAR
VERSION:2.0
METHOD:PUBLISH
<?php       
define('CRLF',"\r\n");
global $base_url;
$ical_time_format='Ymd\THis\Z';

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

function ical_shorten_line($line){
    $line=str_replace(array("\r\n","\n"),'\n',$line);
    return trim(chunk_split($line,76,"\r\n "));
}

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

foreach ($view->result as $node){
        $data=$node->_field_data['nid']['entity'];
//      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;
} ?>
END:VCALENDAR

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, privacy policy and cookie policy

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