The Problem
Our client wanted comments from multiple languages to appear together in one comment stream regardless of which translation of the node you were looking at. The current Multilingual system simply creates new nodes for each translation you create and as such if you comment on an English node it only shows on that node.
The Strategy
After reviewing our options we decided the best implementation strategy would be to have all the comments submitted and associated with one node and load them from that node for any translation of the node that is viewed. One of the main reasons for this was being able to maintain the hierarchical structure of nested comments which would become a nightmare to try and manage ourselves. Unfortunately this implementation method also requires us to hack comment.module, which is unfortunate but necessary to meet our neeeds
Implementation
The first thing to modify is the comment_save function. Whenever a comment is saved we want to check if the node that was being viewed is the original translated node. If it is process as normal, if not we set the nid to the original node.
<?php
// If this isn't the originally translated node, get that NID and use it instead to save the comment
$node = node_load($edit['nid']);
if ($node->tnid && ($node->tnid != $node->nid)) {
$edit['nid'] = $node->tnid;
}
?>
Added on line 700 of comment.module
Now we can submit a comment on a node or any of it's translations and the comments will be applied to the original translated node. The next step is to force comment.module to display the comments on all of the translated nodes. To do this we need to modify the comment_render function.
<?php
// Get all the translation nid's associated with this node
if ($node->tnid != 0) {
$translations = translation_node_get_translations($node->tnid);
$tnids = array();
foreach ($translations as $translation) {
$tnids[] = $translation->nid;
}
}
?>
Added on line 937 of comment.module This code will retrieve a listing of all the translation nodes for the original translated content.
<?php
// Displaying all comments together
if (!empty($tnids)) {
foreach ($tnids as $tnid) {
if ($tnid != $node->nid) {
$query .= ' OR c.nid = %d';
$query_count .= ' OR c.nid = %d';
$query_args[] = $tnid;
}
}
}
?>
Added on line 982 of comment.module this code will go through the list of nodes we retrieved in the previous section and add all the nid's to the comment query. This addition to the query is what makes the comments load on any of the translated nodes in addition to the original node.
Thoughts and considerations
It sucks that this can't be done through methods other than hacking comment.module. Originally I thought views could be used to replace the comment rendering functionality. I think that still might be a possibility, but then we have to re-create all the basic comment functionality that comment module gives us. If anyone has other thoughts on how this could be done without hacking the module we would love to hear it.
Comments
Very interesting! We have
Very interesting!
We have similar needs, but are wary of patching core modules too.
Where do you intend to go with this ? Leave it as is or polish it a bit further ?
A nice option would be to make these changes switchable trough a setting (a global setting, I don't think there's much use to allow per-node choice, at least not during a first phase)
This would be a bit more complex, but would allow to ask for inclusion of that functionality in the core comments module. 8)
Another option would be to make an alternative comments module that mixes comments from all languages on a translated nodes set, but it seems less practical to fork the comments module, and would lead to all kinds of unwanted trouble and maintenance hassle. :((
Thanks, this is exactly what
Thanks, this is exactly what we also need due to strict client requirement. Its very helpful !
Im having problem hacking my drupal. Im on drupal 6.20
Could you please specify which drupal version you were using when carrying out this hack and exactly where to add those codes? (i.e.: for "Added on line 700 of comment.module" what is the code in the line above it (669) or below it(701))
Thank you very much,
BIlly
diff against Drupal 6.22
Here's a diff thats works for me.
I wonder if a GIT sandbox could be use for such core patches... assuming one doesn't get flamed to death for suggest a core patch :-)
(update 21.7.11: used "diff -crB" for a cleaner patch, moved comment display section above admin comments, add some disabled watchdogs that are useful for tracing. A pity attachments are not possible on this node).
diff ORIG NEW
diff -crB /tmp/comment/comment.module /var/www/modules/comment/comment.module:
could you update it for newer 6.xx ?
Seems the comments file has been changed between 6.22 and the current 6.28.
Could this be made into a module or applied to core in some way ?
It's problem for each update, and the function is really great so t'd be too bad to have to lose it :(