function comment_form_submit

You are here

7 comment.module comment_form_submit($form, &$form_state)
4.7 comment.module comment_form_submit($form_id, $form_values)
5 comment.module comment_form_submit($form_id, $form_values)
6 comment.module comment_form_submit($form, &$form_state)

Process comment form submissions; prepare the comment, store it, and set a redirection target.

File

modules/comment/comment.module, line 1539
Enables users to comment on published content.

Code

function comment_form_submit($form, &$form_state) {
  _comment_form_submit($form_state['values']);
  if ($cid = comment_save($form_state['values'])) {
    $node = node_load($form_state['values']['nid']);
    // Add 1 to existing $node->comment count to include new comment being added.
    $comment_count = $node->comment_count + 1;
    $page = comment_new_page_count($comment_count, 1, $node);
    $form_state['redirect'] = array('node/' . $node->nid, $page, "comment-$cid");
    return;
  }
}

Comments

how to change redirection

To change redirection:

<?php
function HOOK_form_comment_form_alter(&$form, &$form_state) {
 
$node = node_load($form['nid']['#value']);
  if (
$node->type == 'MY_NODE_TYPE' && $node->comment) {
   
$form['#redirect'] = array('NEW_REDIRECT_PATH');
  }
}
?>

... for some reason, redirecting was more difficult for me than just the above code. maybe that's because i'm not too familiar with drupal yet. here's what i did for all those who are still searching for a quick but working option.

(info:
i'm doing this on drupal 6. actually i want to change the redirect value to FALSE so that the user is not redirected after comment submission because most of my nodes' functionality - toggle etc. - happens on the frontpage. at the moment, it seems, setting the value to FALSE makes comment_form fall back on $form['#action'], which is not the desired result, but i'm still working on that.)

the problem with $form['#redirect'] and using hook_form_alter is that comment_form doesn't seem to have that value. at least, there is no such value if you output all form values.

(for more details on how to do that, have a look at http://www.lullabot.com/articles/modifying-forms-drupal-5-and-6 )

instead, apparently comment_form calls comment_form_submit through the submit value, where the latter function contains the relevant redirect value. so, the key idea is to create a custom module, which is such that it contains a custom comment_form_submit function and, additionally, it changes the submit value of comment_form by means of hook_form_alter to call that custom comment_form_submit function. i got this idea from john fiala's reply to the following question:

http://stackoverflow.com/questions/4329160/how-do-you-override-a-functio...

now, to the code (note that the custom submit function and the standard one are identical except for the redirect value):

<?php
function YOURCUSTOMMODULENAME_comment_form_submit($form, &$form_state) {
_comment_form_submit($form_state['values']);
  if (
$cid = comment_save($form_state['values'])) {
   
$node = node_load($form_state['values']['nid']);
   
// Add 1 to existing $node->comment count to include new comment being added.
   
$comment_count = $node->comment_count + 1;
   
$page = comment_new_page_count($comment_count, 1, $node);
   
$form_state['redirect'] = YOURREDIRECTIONPATH;
    return;
  }
}

function

YOURCUSTOMMODULENAME_form_alter(&$form, $form_state, $form_id) {
     switch (
$form_id) {
     
// This is our form ID.
     
case 'comment_form':
       
$form['#submit'][] = 'YOURCUSTOMMODULENAME_comment_form_submit';
        break;
    }
}
?>

note that the closing tag "?>" should not appear in the custom module. it's just inserted here for layout convenience.