user1637231
user1637231

Reputation: 151

Pass field content as variable from block preprocessor to block tpl

I have a preprocess function:

[MYTHEMENAME]_theme_preprocess_views_view_fields__random_quote__block(&$vars)

and then a template file to render the variables:

views-view--random_quote--block.tpl.php

I can easily set a variable like so:

$vars['bam'] = 'whatever';

and display that in my template file. Now my question is, how do I pass the contents of a field to my template? Something like:

$vars['customer_name'] = 'field_customer_name';

Where 'field_customer_name' is a field in the content type. I have tried using the field api and I am getting nowhere. My view is getting that field and I can see the data in the preview of the view content

Upvotes: 0

Views: 498

Answers (2)

user1637231
user1637231

Reputation: 151

Ok, I figured out how to do it!

$entity = $vars['view']->result[0]->_field_data['nid']['entity'];
$vars['customer_name'] = '$entity->field_customer_name[$entity->language][0]['value'];

Hope this helps someone

Upvotes: 1

user1637231
user1637231

Reputation: 151

I still haven't figure out how to do this, but I ended up using a fields tpl (in this case: views-view-fields--random_quote.tpl.php) so I could format each field like so

<cite><b><?php print strip_tags($fields['field_customer_name']->content); ?></b>

Upvotes: 1

Related Questions