1. /**
  2. * Previews the comment.
  3. */
  4. function modern_comments_preview_submit($form, &$form_state) {
  5. // Return the actual form if it contains errors.
  6. if (form_get_errors()) {
  7. return $form;
  8. }
  9. $comment = comment_form_submit_build_comment($form, $form_state);
  10. $comment_pre_render = comment_preview($comment);
  11. $comment_output = drupal_render($comment_pre_render['comment_preview']);
  12. $node = $form['#node'];
  13. // This is a reply.
  14. if (isset($form_state['values']['pid'])) {
  15. $form_el = "#comment-id-{$comment->pid} form.comment-form";
  16. $new_form_state = array();
  17. $new_form_state['build_info']['args'][] = (object) array('nid' => $node->nid);
  18. $new_form_state['input'] = $form_state['input'];
  19. $form['#action'] = '/modern_comments/reply/' . $comment->nid . '/' . $comment->pid;
  20. $new_form_build = drupal_build_form($form['#form_id'], $new_form_state);
  21. // Don't build comment and body.
  22. unset($new_form_build['comment_preview']);
  23. unset($new_form_build['comment_output_below']);
  24. $new_form_output = drupal_render($new_form_build);
  25. $commands[] = ajax_command_prepend($form_el, $comment_output);
  26. }
  27. // Or is this a brand new comment.
  28. else {
  29. $new_form_state = array();
  30. $new_form_state['build_info']['args'][] = (object) array('nid' => $node->nid);
  31. $new_form_state['input'] = $form_state['input'];
  32. $new_form_build = drupal_build_form($form['#form_id'], $new_form_state);
  33. // Don't build comment and body.
  34. unset($new_form_build['comment_preview']);
  35. unset($new_form_build['comment_output_below']);
  36. $new_form_output = drupal_render($new_form_build);
  37. $form_el = "#comments > form.comment-form";
  38. $commands[] = ajax_command_prepend('#comments > .content', $comment_output);
  39. }
  40. $commands[] = ajax_command_replace($form_el, $new_form_output);
  41. return array('#type' => 'ajax', '#commands' => $commands);
  42. }