I would like to automatically save a CCK field which will be determined by the data in the $node object. Using node_save in this instance will cause an error so I need a way to simply insert the additional field into the database.

One obvious way is to simply call db_insert but I'm not sure that this is the best way to do this since most of the information in this table is automatically generated and I'd like to leave that up to Drupal if I can.

What is that standard approach in this case?

Thanks!

share|improve this question

1 Answer

up vote 1 down vote accepted

You could probably do this in hook_node_presave(), e.g.:

function MYMODULE_node_presave($node) {
  // Get a value made up of other submitted fields, e.g.
  $computed_val = $node->field_first[$node->language][0]['value'] + $node->field_second[$node->language][0]['value'];

  // Assign that value to the other field
  $node->field_computed_field[$node->language][0]['value'] =  $computed_val;
}
share|improve this answer
Thanks, I actually just found this myself this. – Godwin Feb 8 '12 at 22:19

Your Answer

 
or
required, but never shown
discard

By posting your answer, you agree to the privacy policy and terms of service.

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