views:

190

answers:

1

I am creating a module that needs to tag nodes with taxonomy terms when they are created. I have implemented hook_nodeapi() to do this, and am trying to add the term in there like so:

function hook_nodeapi(&$node, $op, $a3 = NULL, $a4 = NULL) {
  switch ($op) {
    case 'insert':
      $node->taxonomy = array(1 => taxonomy_get_term(1));
      node_save($node);
    break;
  }
}

When I call node_save(), the code just goes in to a loop, because node_save calls hook_nodeapi(). When I don't call node_save, nothing is saved. I am durned if I do, durned if I don't. Any ideas on the right way to do thi?

+2  A: 

You could use $op="presave" to alter the taxonomy in the way you want and then let the taxonomy module save the terms for you. No need to use node_save afterwards. In fact node save should be avoided in nodeapi implementations for the reasons you state.

Jeremy French

related questions