- /**
- * Previews the comment.
- */
- function modern_comments_preview_submit($form, &$form_state) {
- // Return the actual form if it contains errors.
- if (form_get_errors()) {
- return $form;
- }
- $comment = comment_form_submit_build_comment($form, $form_state);
- $comment_pre_render = comment_preview($comment);
- $comment_output = drupal_render($comment_pre_render['comment_preview']);
- $node = $form['#node'];
- // This is a reply.
- if (isset($form_state['values']['pid'])) {
- $form_el = "#comment-id-{$comment->pid} form.comment-form";
- $new_form_state = array();
- $new_form_state['build_info']['args'][] = (object) array('nid' => $node->nid);
- $new_form_state['input'] = $form_state['input'];
- $form['#action'] = '/modern_comments/reply/' . $comment->nid . '/' . $comment->pid;
- $new_form_build = drupal_build_form($form['#form_id'], $new_form_state);
- // Don't build comment and body.
- unset($new_form_build['comment_preview']);
- unset($new_form_build['comment_output_below']);
- $new_form_output = drupal_render($new_form_build);
- $commands[] = ajax_command_prepend($form_el, $comment_output);
- }
- // Or is this a brand new comment.
- else {
- $new_form_state = array();
- $new_form_state['build_info']['args'][] = (object) array('nid' => $node->nid);
- $new_form_state['input'] = $form_state['input'];
- $new_form_build = drupal_build_form($form['#form_id'], $new_form_state);
- // Don't build comment and body.
- unset($new_form_build['comment_preview']);
- unset($new_form_build['comment_output_below']);
- $new_form_output = drupal_render($new_form_build);
- $form_el = "#comments > form.comment-form";
- $commands[] = ajax_command_prepend('#comments > .content', $comment_output);
- }
- $commands[] = ajax_command_replace($form_el, $new_form_output);
- return array('#type' => 'ajax', '#commands' => $commands);
- }