I have to upgrade a large database from 6.x to 7.x and the cck content type has fields that must to be included beforeinsert.
In drupal 6.x I just used hook_nodeapi like this:

<?php
// hook_nodeapi implementation.
function moc_nodeapi(&$node, $op, $a3 = NULL, $a4 = NULL){
  if(
$node->type == 'moc'){
    if(
$op == 'insert'){
     
$node->field_moc_clave[0]['value'] = 'New clave'; // Document code
     
....
    }
  }
}
?>

And it works fine but ...

In drupal 7.x I intent to do the next:

<?php
// hook_node_insert() implementation.
function moc_node_insert($node) {
 
$node->field_moc_clave['und'][0]['value'] = 'New clave'; // Document code
 
...
}
?>

And it doesn't works.

How do I change cck fields values before they are inserted into database?
And the same way from hook_nodeapi() op='update' ?

Comments

If you want to alter a node value before it's inserted into database and check if its inserted by first time(new) or it's being updated, you could use the next code:

<?php
// Implements hook_node_presave().
function MODULE_node_presave($node) {
  if (
$node->is_new) {
 
// Its a new node, first time that is inserted to db
 
}else{
 
// It's being updated.
 
}
}
?>

Please check node_save() function for more information.

And if you are shocked because the function has no "return" and $node parameter hasn't '&' to make it a parameter by reference, like in Drupal 6, please read this comment: here