如何以编程方式将分类术语添加到 hook_nodeapi() 中的节点?

发布于 2024-09-10 12:34:05 字数 450 浏览 11 评论 0原文

我正在创建一个模块,需要在创建节点时用分类术语标记节点。我已经实现了 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

内心荒芜 2024-09-17 12:34:05

您可以使用 $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.

故事↓在人 2024-09-17 12:34:05

使用 hook_form_alter() 将其添加到 $form_state

use a hook_form_alter() to add it to the $form_state

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文