tags:

views:

733

answers:

2

I'm trying to show updated results for a CCK Computed Field.

The computation is based on fields in another node, so are not being automatically updated.

So: I'm calling node_save($node) in hook_view, which does make the adjustment but the results don't show until I refresh the page.

Is there a way to refresh the page automatically, or should I be approaching this from a different angle?

Edit: In response to Henrik's questions, here's more detail:
The hook_view and its node_save are below, the rest of the code is in a Computed Field in the 'project' content type, summing up values from another node. Without the node_save, I have to edit and save the 'project' node to get the result. With it, I just need to refresh the page.

Adding drupal_goto(drupal_get_destination()) in the hook_view gives a 'page not found', rather than the vicious loop I was expecting. Is there another place I could put it?

function mymodule_nodeapi(&$node, $op, $teaser = NULL, $page = NULL) {
  switch ($op) {
    case 'view':
      if($node->type == 'project') {
        project_view($node);
      break;
      }
  }
}

function project_view($node) {
    node_save($node);
    return $node;
}
+1  A: 
Henrik Opel
+1  A: 

If the value is only ever going to be computed, you could just add something to your node at load time.

function mymodule_nodeapi(&$node, $op, $teaser = NULL, $page = NULL) {
  switch ($op) {
    case 'load':
      if($node->type == 'project') {
         $node->content['myfield'] = array('#value' => mymodule_calculate_value(), '#weight' => 4, '#theme' => 'my_theme');  
       }
      break;
      }
  }
}
Jeremy French
This is a good alternative, but it's frustrating that I can't work out how to do it directly with the Computed Field.
lazysoundsystem

related questions