如何覆盖另一个模块中存在的函数

发布于 2024-10-05 07:08:50 字数 207 浏览 1 评论 0原文

我想重写 comment_save 函数。有没有办法覆盖它以便我可以添加我的功能?

创建诸如 http://drupal.org/node/375833 之类的触发器是最好的方法吗?这似乎不太可靠,因为它是事后发生的。

顺便说一句,这是在 D6 中。

I would like to override the comment_save function. Is there a way to override it so I can add in my functionality?

Is creating a trigger such as http://drupal.org/node/375833 the best way? That doesn't seem very reliable since it happens after the fact.

BTW, this is in D6.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

久随 2024-10-12 07:08:50

这很棘手。首先,我建议您去看看是否可以使用 hook_comment 来管理您需要的内容( http://api.drupal.org/api/drupal/developer--hooks--core.php/function/hook_comment/6)。这实际上更多的是需要的。

如果您确实需要覆盖comment_save,那么通常一个不错的选择是看看它的名称。对于这种情况,你很幸运 - comment_save 只在一个地方被调用,即 comment.module 的第 1542 行,comment_form_submit 内部。

现在,comment_form_submit是一个表单提交函数 - 我们可以制作自己的自定义版本的comment_form_submit,而不是替换comment_save(为了论证,我们假设我们正在“custom_module”模块中工作) - 所以我会做什么要做的是创建一个名为 custom_module_comment_form_submit 的新函数,它调用我感兴趣的 comment_save 的变体,然后使用 hook_form_alter() ( http://api.drupal.org/api/drupal/developer--hooks--core.php/function/hook_form_alter/6 )设置 $form['#submit'] 将值 'comment_form_submit' 替换为 'custom_module_comment_form_submit'。

这并不完美 - 如果您安装另一个使用 comment_save 的模块,那么您将需要找到类似的解决方法。如果在 Drupal 中发现错误或安全漏洞,并在您正在处理的部分中修复该漏洞,那么您的代码中将无法修复该安全漏洞。但如果你绝对必须替换 comment_save,这几乎是唯一的方法。

It's tricky. First off, I'd suggest instead going and seeing if you can manage what you need by using hook_comment ( http://api.drupal.org/api/drupal/developer--hooks--core.php/function/hook_comment/6). It's really more of what this is needed for.

If you really need to override comment_save, then often a good choice is to look around at what things call it. For this situation, you're lucky - comment_save only gets called in one place, on line 1542 of comment.module, inside of comment_form_submit.

Now, comment_form_submit is a form submit function - instead of replacing comment_save, we can instead make our own custom version of comment_form_submit (and for argument's sake, we'll assume we're working in a 'custom_module' module) - so what I would do is create a new function called custom_module_comment_form_submit, which calls the variant of comment_save that I'm interested in using, and then use hook_form_alter() ( http://api.drupal.org/api/drupal/developer--hooks--core.php/function/hook_form_alter/6 ) to set the $form['#submit'] to replace the value 'comment_form_submit' with 'custom_module_comment_form_submit'.

This isn't perfect - if you install another module that uses comment_save, then you'll need to find a similar workaround. And if a bug or security hole is found in Drupal that is fixed in the part you're working around here, you won't get that security hole fixed in your code. But if you absolutely must replace comment_save, this is pretty much the only way to do it.

眼角的笑意。 2024-10-12 07:08:50

您无法“覆盖”comment_save 函数,但可以在自定义模块中实现 hook_comment。

请参阅 drupal.org 文档关于这个话题。

对您来说方便的两个 $ops 是:

  • “insert”:正在插入注释。
  • “update”:评论正在更新。

希望这有帮助

You cannot 'override' the comment_save function, but you can implement the hook_comment in a custom module.

See drupal.org's documentation on the topic.

The two $ops in question that would be handy for you would be:

  • "insert": The comment is being inserted.
  • "update": The comment is being updated.

Hope this helps

千と千尋 2024-10-12 07:08:50

虽然肯定不理想,但有时我会创建自己的自定义模块,其中包含修改后的函数,然后注释掉原始模块(并添加足够的注释来告诉其他人这样做的原因。)这样,如果您忘记并升级模块如果其中包含原始函数,它会抛出一个错误,指出该函数已被声明,提醒您重新评估或至少重新注释它。钩子当然是最佳实践,但在紧要关头,这至少给你带来了比在现有模块中实际编辑函数更好的东西。在某些情况下,我发现这对于使某些模块能够很好地协同工作是必要的。

While certainly not ideal, at times I've created my own custom module with the modified function in it and then commented out the original (and added sufficient comments to tell others why this was done.) This way if you forget and upgrade the module that has the original function in it down the road it will throw an error saying the function has already been declared reminding you to re-evaluate or at least re-comment it. Hooks are certainly the best practice, but in a pinch this at least gives you something slightly better than actually editing the function in an existing module. In a few cases I've found this necessary to get certain modules to play nicely together.

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