Reputation: 2434
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.
Upvotes: 2
Views: 6539
Reputation: 581
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.
Upvotes: 1
Reputation: 4581
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.
Upvotes: 5
Reputation: 3798
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:
Hope this helps
Upvotes: 2