I can't figure out why my code won't update the Views content used outside this function. It works fine inside, but has no effect when the field is referenced outside. I have tried using variable references (&$res, &$view), but it had no effect. Here is my code:

function mymodule_views_pre_render($view) {
	
	foreach($view->result as $key => $res) {
		if ($res->_field_data['nid']['entity']->type == "myentity") {	
			// do some stuff
			
			$res->_field_data['nid']['entity']->field_myfield['und'][0]['value'] = "Blah"; 
			drupal_set_message('<pre>' . "Test output: " . $res->_field_data['nid']['entity']->field_myfield['und'][0]['value'] . '</pre>');
		}
	}
}

It produces "Test output: Blah" like it is supposed to, but has no effect on the field when rendered in the View results.

Thanks.

Comments

bhosmer’s picture

I think you need to access the field's rendered markup like this:

foreach ($view->result as $rownumber => $row) {
  $row->field_field_myfield[0]['rendered']['#markup'] = 'Blah';
}

You also might find the Devel module really helpful.

You can easily look at the values like this:

hook_views_pre_render(&$view) {
  dsm($view);
}