having fun with code

Ajax comments in Drupal 5: How I made it – Part I

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.

  1. function spaniards_menu($may_cache) {
  2.   $items = array();
  3.   if (!$may_cache) {
  4.     // Add comment
  5.     $items[] = array(
  6.       'path' => 'add/comment',
  7.       'title' => 'Add Comment',
  8.       'callback' => 'spaniards_add_comment',
  9.       'callback arguments' => array(),
  10.       'access' => user_access("post comments"),
  11.       'type' => MENU_CALLBACK,
  12.     );
  13.   }
  14. }
  15.  
  16. function spaniards_add_comment() {
  17.    global $user;
  18.    $text = $_REQUEST['text'];
  19.    $comment = array();
  20.    $comment['uid'] = $user->uid;
  21.    $comment['nid'] = $_REQUEST['nid'];
  22.    $comment['pid'] = 0;
  23.    $comment['subject'] = substr($text, 0, 20);
  24.    $comment['comment'] = $text;
  25.    // Add comment to DB
  26.    $cid = comment_save($comment);
  27.  
  28.    // Mark the node as viewed by the user
  29.    node_tag_new($comment['nid']);
  30.  
  31.    // Get all comments since the last one
  32.    $fromcid = isset($_REQUEST['lastcid'])? $_REQUEST['lastcid'] : 0;
  33.    print _spaniards_get_latest_comments($comment['nid'], $fromcid);
  34.  
  35.    exit;
  36. }

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

Related Posts:

10 Comments to Ajax comments in Drupal 5: How I made it – Part I

  1. December 15, 2008 at 09:25 | Permalink

    TEST

  2. drupal-tester's Gravatar drupal-tester
    December 23, 2008 at 07:31 | Permalink

    sorry… thi is not a spam. I’m just going to test this AJAX comments ;)

  3. yvonne's Gravatar yvonne
    February 11, 2009 at 19:24 | Permalink

    asdasd

  4. test's Gravatar test
    April 18, 2009 at 19:17 | Permalink

    testaaaaaa

  5. asgsad's Gravatar asgsad
    April 26, 2009 at 13:55 | Permalink

    fdsgsdf

  6. May 22, 2009 at 00:40 | Permalink

    нет, не всегда. но в этом случае, да.

  7. November 13, 2010 at 03:04 | Permalink

    test, test

  8. akb's Gravatar akb
    January 14, 2011 at 19:08 | Permalink

    Hello 1 2 3

  9. June 21, 2011 at 07:04 | Permalink

    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?

Leave a Reply

You can use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Additional comments powered by BackType

About the blog

This is a blog about development, focused mainly on Javascript but also other languages like python, shell scripts and more.

About the author

Eneko Alonso is a software engineer and UI developer with more than eight years of experience in software and web development. He lives in San Luis Obispo, California and works at LEVEL Studios.

Contact Info

Contact Info

PromoteJS

JavaScript JS Documentation