Data updated in views->result does not get rendered

pernaletec's picture

Dear drupalers,

During the hook_post_execute process I'm trying to update some node fields. The idea is to make some calculations using data from views->result and then update the nodes. The node fields I want to update are part of the query too, and at the end of the story I need the data updated rendered.

Below there is an example of the piece of code I'm trying:

<?php
function my_module_views_post_execute(&$view){
  if (
$view->human_name=='my_view'){
   
$nid_node = $view->result[0]->nid;
   
$mynode = node_load($nid_node);
   
$var = $view->result[0]->_field_data['nid']['entity']->field_my_first_field['und'][0]['value'];
   
$var_two = $var * 2;
   
$init = array(
     
'und' => array(
       
0 => array (
         
'value'=> 0.000
         
)
        )
      );
   
//Some times this field is empty
   
if (empty($mynode->field_my_second_field)) {
     
$mynode->field_my_second_field['und'][0]['value'] =$init;
    }
   
$mynode->field_my_second_field = $var_two;
   
dfb($view->result[0],'$view->result[0] after update');
   
node_save($mynode);
    }
}
?>

Note that I check if the field field_my_second_field is empty because sometimes it is. That happens because this field was created in the node configuration GUI with no value. In this case I inicialize first the field with $init and then assign the data. The issue is that the field field_my_second_field does not get rendered after being updated. However it does get rendered if the page is refreshed. That makes me think that the data is being stored in the data base but is not taken into account in the render process just after being updated.

Is important to notice that when I check $views->result after updating the data, it contains the data updated.

Someone could please give some hint about what could be happening?

The views version is 7.x-3.7.

Thanks in advance!