You are here

function better_comments_preview in Better Comments 7.2

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

Previews the comment.

3 string references to 'better_comments_preview'
better_comments_form_comment_form_alter in ./better_comments.module
Implements hook_form_comments_form_alter().
better_comments_settings in ./better_comments.admin.inc
Better comments settings form.
better_comments_uninstall in ./better_comments.install
Implements hook_uninstall().

File

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

Code

function better_comments_preview($form, &$form_state) {
  if (form_get_errors()) {
    $form['comment_body']['#attributes'] = array(
      'class' => array(
        'error',
      ),
    );
    $form = drupal_render($form);
    if (isset($form['cid']['#value'])) {
      $cid = $form['cid']['#value'];
      $commands[] = ajax_command_remove('#comment-wrap-' . $cid . ' .comment-form', $form);
    }
    elseif (isset($form_state['values']['pid'])) {

      // Append comment to parent wrapper.
      $pid = $form_state['values']['pid'];
      $commands[] = ajax_command_replace('#comment-wrap-' . $pid . ' .comment-form', $form);
    }
    else {
      $commands[] = ajax_command_replace('.comment-form', $form);
    }
  }
  else {
    if (isset($form['cid']['#value'])) {
      $cid = $form['cid']['#value'];
    }
    $comment = $form_state['comment'];
    $node = $form['#node'];
    $comment_build = comment_view($comment, $node);
    $comment_build['#prefix'] = '<div class="preview better-comments-confirm-' . $cid . '">';
    $comment_build['links']['comment']['#links']['comment-delete'] = FALSE;
    $comment_output = drupal_render($comment_build);

    // Disable the preview buttom on form.
    // Add a new form and not from cached.
    $form_state = array();
    $form_state['input'] = array();
    $form_state['comment'] = $comment;
    $form_state['build_info']['args'][] = (object) array(
      'nid' => $node->nid,
    );
    $form_build = drupal_build_form($form['#form_id'], $form_state);
    $form_build['actions']['preview']['#access'] = FALSE;

    // Check if comment is being edited.
    if (isset($form['cid']['#value'])) {
      $cancel_url = 'better_comments/preview/' . $node->nid . '/cancel/' . $form['cid']['#value'];
    }
    elseif (isset($form_state['values']['pid'])) {
      $cancel_url = 'better_comments/reply/preview/cancel/' . $form_state['values']['pid'];
    }
    else {
      $cancel_url = 'better_comments/preview/' . $node->nid . '/cancel';
    }

    // Add a cancel preview button to the form.
    $form_build['actions']['cancel'] = array(
      '#markup' => l(t('Cancel'), $cancel_url, array(
        'attributes' => array(
          'class' => array(
            'use-ajax',
            'button',
            'preview-cancel',
          ),
          'id' => array(
            'preview-' . $form_state['values']['pid'],
          ),
        ),
      )),
      '#weight' => 21,
    );
    $form = drupal_render($form_build);

    // Check if it is a preview of edit form.
    if (isset($cid)) {
      $commands[] = ajax_command_replace('#comment-wrap-' . $cid . ' .comment-form', $form);
      $commands[] = ajax_command_prepend('#comment-wrap-' . $cid, $comment_output);
    }
    elseif (isset($form_state['values']['pid'])) {

      // Append comment to parent wrapper.
      $pid = $form_state['values']['pid'];
      $commands[] = ajax_command_replace('#comment-wrap-' . $pid . ' .comment-form', $form);
      $commands[] = ajax_command_prepend('#comment-wrap-' . $pid . '> .indented-' . $pid, $comment_output);
    }
    else {
      $commands[] = ajax_command_replace('.comment-form', $form);
      $commands[] = ajax_command_prepend('.comment-form', $comment_output);
    }
  }
  return array(
    '#type' => 'ajax',
    '#commands' => $commands,
  );
}