How can I programmatically add taxonomy terms to a node in hook_nodeapi()?
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_g开发者_如何学Goet_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?
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.
use a hook_form_alter()
to add it to the $form_state
精彩评论