You are here

function better_comments_submit in Better Comments 7.2

Same name and namespace in other branches
  1. 7 better_comments.module \better_comments_submit()

Submit the comment.

1 string reference to 'better_comments_submit'
better_comments_form_comment_form_alter in ./better_comments.module
Implements hook_form_comments_form_alter().

File

./better_comments.module, line 443
Better Comments provides option to configure the comment system.

Code

function better_comments_submit(&$form, &$form_state) {
  if (form_get_errors()) {
    $form['#attributes'] = array(
      'id' => array(
        'edit-comment-body',
        'error',
      ),
    );

    // Check if this a replay form.
    if (isset($form_state['values']['pid'])) {
      $pid = $form_state['values']['pid'];
      $form['actions']['cancel'] = array(
        '#markup' => l(t('Cancel'), 'better_comments/reply/' . $pid . '/cancel', array(
          'attributes' => array(
            'class' => array(
              'use-ajax',
              'button',
            ),
          ),
        )),
        '#weight' => 21,
      );
      $form = drupal_render($form);
      $commands[] = ajax_command_replace('#comment-wrap-' . $pid . '> .comment-form', $form);
    }
    else {
      $form = drupal_render($form);
      $commands[] = ajax_command_replace('.comment-form', $form);
    }
    return array(
      '#type' => 'ajax',
      '#commands' => $commands,
    );
  }
  $form_state['redirect'] = '';

  // Unset the preview mode for form submit.
  unset($form_state['comment']->in_preview);
  unset($form_state['comment']->preview);
  $comment = $form_state['comment'];
  $node = $form['#node'];
  $comment_build = comment_view($comment, $node);
  $comment_output = drupal_render($comment_build);

  // Existing comment is edited.
  if (isset($form['cid']['#value'])) {
    $commands[] = ajax_command_remove('.better-comments-confirm-' . $comment->cid);
    $commands[] = ajax_command_replace('.comment-inner-' . $comment->cid, $comment_output);
  }
  elseif (isset($form_state['values']['pid'])) {

    // Else this is a reply.
    // Append comment to parent wrapper.
    $comment_output = '</div>' . $comment_output;
    $commands[] = ajax_command_replace('.indented-' . $comment->pid, $comment_output);

    // Delete the form.
    $commands[] = ajax_command_invoke('#' . $form['#id'], 'remove');
  }
  else {
    $commands[] = ajax_command_append('.comment-section', $comment_output);
  }

  // Add a new empty form and not the cached form with comment body text.
  $form_state = array();
  $form_state['input'] = array();
  $form_state['build_info']['args'][] = (object) array(
    'nid' => $node->nid,
  );
  $form_build = drupal_build_form($form['#form_id'], $form_state);
  $form = drupal_render($form_build);
  $commands[] = ajax_command_replace('.comment-form', $form);
  return array(
    '#type' => 'ajax',
    '#commands' => $commands,
  );
}