如何以编程方式将分类术语添加到 hook_nodeapi() 中的节点?
我正在创建一个模块,需要在创建节点时用分类术语标记节点。我已经实现了 hook_nodeapi() 来执行此操作,并尝试在其中添加术语,如下所示:
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;
}
}
当我调用 node_save() 时,代码只是进入循环,因为 node_save 调用 hook_nodeapi()。当我不调用node_save时,不会保存任何内容。如果我这样做,我就会受到诅咒,如果我不这样做,我就会受到诅咒。关于正确的方法有什么想法吗?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用
$op="presave"
按照您想要的方式更改分类,然后让分类模块为您保存术语。之后不需要使用node_save。事实上,出于您所述的原因,在 nodeapi 实现中应该避免节点保存。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.使用
hook_form_alter()
将其添加到$form_state
use a
hook_form_alter()
to add it to the$form_state