First of all, I would like to apologize for not publishing this the right way. I haven’t had time to put it all on a standalone module, which will make it easier to share the code at www.drupal.org. Feel free to grab the code and put it on a module if you wish (and let me know if you do so :)
Why Ajax comments?
Ajax comments let users to submit comments without having to reload the whole page. Reloading a page is not only slow, but its an overload for the server which has to process and send the whole page again just to add a simple comment to the DOM. So yes, Ajax comments are a big benefit, both for the user, which has to wait less, and for the server, which has to process and send less information.
Available modules
Point explained, lets see how it went. In the past I tried the Javascript Tools set of modules, which had other dependencies if I am not mistaken. And I don’t know why but it didn’t work at all for me. My comments kept reloading the whole page while sent. A couple of months ago I decided it was time to do it myself.
The plan
My original plan was simple: when the user hits the submit button, send the comment to the server, save it and add it to the DOM at the end of the comment list. This looked simple, but it had a few thing to deal with. I didn’t want to mess with Drupal’s comment module neither modify any line of Drupal’s core. So I decided to take an alternative path.
Saving a comment
Saving a comment doesn’t require much information. Since the user has to be logged in, the only information needed is basically the node Id and the comment itself. Drupal has a very nice hook_menu that allows you to create pages with a specific URL. These pages can process data, serve data or both. The only drawback, if so, was that hooks only works on modules, so I had to create my own. I decided to add it to my custom site module.
-
function spaniards_menu($may_cache) {
-
$items = array();
-
if (!$may_cache) {
-
// Add comment
-
$items[] = array(
-
'path' => 'add/comment',
-
'title' => 'Add Comment',
-
'callback' => 'spaniards_add_comment',
-
'callback arguments' => array(),
-
'access' => user_access("post comments"),
-
'type' => MENU_CALLBACK,
-
);
-
}
-
}
-
-
function spaniards_add_comment() {
-
global $user;
-
$text = $_REQUEST['text'];
-
$comment = array();
-
$comment['uid'] = $user->uid;
-
$comment['nid'] = $_REQUEST['nid'];
-
$comment['pid'] = 0;
-
$comment['subject'] = substr($text, 0, 20);
-
$comment['comment'] = $text;
-
// Add comment to DB
-
$cid = comment_save($comment);
-
-
// Mark the node as viewed by the user
-
node_tag_new($comment['nid']);
-
-
// Get all comments since the last one
-
$fromcid = isset($_REQUEST['lastcid'])? $_REQUEST['lastcid'] : 0;
-
print _spaniards_get_latest_comments($comment['nid'], $fromcid);
-
-
exit;
-
}
Just with those two functions we can bypass Drupal’s comment submission system and add new comments to the database. As long as we use the comment_save function, the actual process involved in saving a comment is safe (hooks get called, watchdog logging works, etc). Also, hook_menu guarantees only requests with the right access will be able to add a comment (in this case the user_access function verifies the user has “post comments” permissions granted).
Once the comment is saved, the node is marked as viewed by the user, to remove all “new” labels from the new comments added.
Finally the comment is rendered and returned to the user. But wait! What if another user has posted a comment in between? During a conversation between multiple users this will happen very often. The solution: return all comments added since the last comment visible when the user visited the thread.
We will see how to do that next.
Series:
Ajax comments in Drupal 5: How I made it – Part I
Ajax comments in Drupal 5: How I made it – Part II
Ajax comments in Drupal 5: How I made it – Part III
See the ajax comments in action
Please, note this is a WordPress based blog. The Drupal website where I have implemented the ajax comments is www.spaniards.es

TEST
Please, note this is a WordPress based blog. The Drupal website where I have implemented the ajax comments is http://www.spaniards.es
sorry… thi is not a spam. I’m just going to test this AJAX comments ;)
asdasd
testaaaaaa
fdsgsdf
нет, не всегда. но в этом случае, да.
test, test
Hello 1 2 3
Thanks for the tip, man. This is exactly what I was looking for. I’ve been having all sorts of problems with Ajax though. It’s an error or something. What should I do?