Community

custom comment module, $edit variable in comment module

Hi Everybody,

I'm writing a custom comment module for my site because of several functionality requirements that do not seem to be provided by other modules I have come across so far. I have a blog-style page where the teasers of a number of posts (using content type 'story' for this) are displayed on the frontpage and if a user clicks on the title of a teaser the teaser disappears and the full node body slides down so that technically users never access single post pages. Now, I want

  • a custom comment form to appear under each post (that is, teaser), plus - for other reasons -
  • I do not want the page to refresh after comment form submission (so I guess I have to use AJAX or AHAH respectively for this feature)

Now, I am having issues grabbing the node id of the post the comment is supposed to be referring to and store it in the database upon form submission (I always get nid = 0 in the database). At first, I thought that was because each form would grab the node id of the frontpage because all the posts and forms are displayed on the frontpage but using the standard comment module resulted in the forms grabbing the correct nid of the node the comment was supposed to be referring to. (Both times I used a comment_render function, that is, custom_module_comment_render function respectively, containing drupal_get_form, printed in the node.tpl.php file to render the form.) Now, I'm a bit clueless as to how to go about this ... any ideas? (Posted parts of my code below.)

Related to that, I'm having problems understanding the code of the standard comment module. Most of it seems clear to me but I can't for the love of god get my head around understanding what the $edit variable is used for in different instances. Sometimes, it seems to be replacing $form[state], but how does it work for example in the comment_form function and where does it come from?

Any help much appreciated!

Best,
the0

module fragments ($kid is the custom comment id):

<?php
// comment form and comment form submission

function custommodule_form(&$form_state, $edit, $title = NULL) {
  global
$user;
 
$date = format_date($edit['timestamp'], 'custom', 'd.m.Y H:i');

 
$op = isset($_POST['op']) ? $_POST['op'] : '';
 
$node = node_load($edit['nid']);

   
$form['name'] = array(
     
'#type' => 'textfield',
     
'#title' => t('Name'),
     
'#maxlength' => 60,
     
'#size' => 30,
     
'#default_value' => variable_get('anonymous', t('Anonymous')),
     
'#required' => TRUE,
    );

   
$form['mail'] = array(
     
'#type' => 'textfield',
     
'#title' => t('E-mail'),
     
'#maxlength' => 64,
     
'#size' => 30,
    );

   
$form['homepage'] = array(
     
'#type' => 'textfield',
     
'#title' => t('Homepage'),
     
'#maxlength' => 255,
     
'#size' => 30,
    );

 
$form['subject'] = array(
     
'#type' => 'textfield',
     
'#title' => t('Subject'),
     
'#maxlength' => 64,
     
'#required' => TRUE,
    );

 
$form['comment'] = array(
   
'#type' => 'textarea',
   
'#title' => t('Comment'),
   
'#rows' => 15,
   
'#required' => TRUE,
  );

 
$form['kid'] = array(
   
'#type' => 'value',
   
'#value' => !empty($edit['kid']) ? $edit['kid'] : NULL,
  );

 
$form['nid'] = array(
   
'#type' => 'value',
   
'#value' => $edit['nid'],
  );

 
$form['uid'] = array(
   
'#type' => 'value',
   
'#value' => !empty($edit['uid']) ? $edit['uid'] : 0,
  );

   
$form['submit'] = array(
     
'#type' => 'submit',
     
'#value' => t('Send'),
     
'#weight' => 19,
    );

 
$form['#token'] = 'custommodule' . $edit['nid'];

  return
$form;
}

function
custommodule_form_submit($form_id, $form_values) {

       
// Add the comment to database.
   
db_query("INSERT INTO {costummodulecomments} (nid, uid, subject, comment, format, hostname, timestamp, status, name, mail, homepage) VALUES (%d, %d, '%s', '%s', %d, '%s', %d, %d, '%s', '%s', '%s')", $edit['nid'], $edit['uid'], $edit['subject], $edit['comment'], $edit['format'], ip_address(), $edit['timestamp'], $edit['status'], $edit['name'], $edit['mail'], $edit['homepage']);
        $edit['
kid'] = db_last_insert_id('custommodulecomments', 'kid');
   
      cache_clear_all();
    drupal_set_message(t('
Your comment has been queued for moderation by site administrators and will be published after approval.'));
    return;
}

// comment rendering

function tenplusfive_kommentar_render($node, $kid = 0) {
  global $user;

  $output = '';
  $output .= custommodule_form_return(array('
nid' => $nid), t('customtext'));
  return $output;
}

/**
* Return comment form.
*/
function tenplusfive_kommentar_form_return($edit, $title = NULL) {
  return drupal_get_form('
tenplusfive_kommentar_form', $edit, $title);
}
?>
nobody click here