The Key to Jquery Form Plugin + Drupal Formapi

Today I bring you an incomplete, yet stunningly easy solution to a problem that's been making want to set buildings on fire.

This problem was making drupal comments go AJAX by way of jquery's form plugin. I had all the good makings of a digg clone comment system, with reply forms dynamically inserting under replies (instead of going to another page). It was just the sans-pagerefresh comment submit thing that was missing.

Tragically, breaking the submit (necessary for ajax) also broke the ability for formapi to do its magic. Then, MerlinofChaos pointed out the 'obvious'approach on IRC.

Use drupal_execute to emulate form submission at a custom callback url. This approach can be applied to any form, so far as I can imagine. So I'm sharing it before I complete the much needed, and long desired ajax_comments.module (yes, it is really coming... solving this problem means I jumped over the last hurdle).

Here's the stripped down, proof of concept code: AJAX_COMMENTS.MODULE

<?php
define('AJAX_COMMENTS_PATH', drupal_get_path('module', 'ajax_comments'));

function ajax_comments_menu($may_cache) {
$items = array();
$items[] = array(
'title' => 'AJAX callback',
'callback' => 'ajax_comments_post',
'path' => 'ajax_comments_post',
'access' => user_access('post comments')
);
return $items;
}

function ajax_comments_post() {
// we submit our form to a special path to account for comments expecting a nid, and pid.....
$args = array('nid' => arg(1), 'pid' => arg(2));
// in that order 1. form_id, 2. $_POST, 3. (etc) additional arguments that the form would expect
drupal_execute('comment_form',$_POST, $args);
// over here you would fire a "response" function that would actually return html, that you would then insert into the page via jquery. Obviously, we aren't going there in this code snippet.
}

/* Now I fully admit THIS code is untested... its just a quick simplification of the code I have behind ajax_comments module at the moment
For this code, the only purpose is to break the default submit, and allow a comment to run through FAPI... everything else is for the released module that's coming.
*/
function ajax_comments_form_alter($form_id, &$form) {
if ($form_id == 'comment_form') {
//include the necessary jquery plugin
drupal_add_js(AJAX_COMMENTS_PATH.'/jquery.form.js', 'module');
// this is a variable, thank god you have me commenting it :-P
$nid = arg(1):
drupal_add_js("
$(document).ready( function() {
var options = {
// this is the url that is define in hook menu as the callback
url: '/ajax_comments_post/'.$nid,
type: 'POST',
clearForm: true ,
resetForm: true,
};
// using variable to show that this is a very general approach
$('$form_id').submit(function() {
$(this).ajaxSubmit(options);
// RIGHT HERE BREAKS SUBMIT
return false;
});
});
", 'inline');
}
}
?>

In conclusion, this code pretends to do nothing besides break a form submit, and allow formapi to nevertheless go about its business doing validate/submit/insert DONE! Its not a complete solution, just the solution to the one remaining bigticket problem that I had building an ajax comments module modeled after the DIGG comment system. Its meant to be useful to people who already are pretty deep into this stuff, and not people who are learning (albeit, I am a javascript woodpusher, and am just pawing around with the JS code, much like a cat pawing its way out of a cage.) Anyways, feedback on what's wrong with the approach is much appreciated, since I am, indeed, a child who shows "great creativity" when it comes to javascript. Cheers.