You are here

function comment_form_add_preview in Drupal 6

Same name and namespace in other branches
  1. 4 modules/comment.module \comment_form_add_preview()
  2. 5 modules/comment/comment.module \comment_form_add_preview()

Form builder; Generate and validate a comment preview form.

Related topics

1 string reference to 'comment_form_add_preview'
comment_form in modules/comment/comment.module
Generate the basic commenting form, for appending to a node or display on a separate page.

File

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

Code

function comment_form_add_preview($form, &$form_state) {
  global $user;
  $edit = $form_state['values'];
  drupal_set_title(t('Preview comment'));
  $output = '';
  $node = node_load($edit['nid']);

  // Invoke full validation for the form, to protect against cross site
  // request forgeries (CSRF) and setting arbitrary values for fields such as
  // the input format. Preview the comment only when form validation does not
  // set any errors.
  drupal_validate_form($form['form_id']['#value'], $form, $form_state);
  if (!form_get_errors()) {
    _comment_form_submit($edit);
    $comment = (object) $edit;

    // Attach the user and time information.
    if (!empty($edit['author'])) {
      $account = user_load(array(
        'name' => $edit['author'],
      ));
    }
    elseif ($user->uid && !isset($edit['is_anonymous'])) {
      $account = $user;
    }
    if (!empty($account)) {
      $comment->uid = $account->uid;
      $comment->name = check_plain($account->name);
    }
    elseif (empty($comment->name)) {
      $comment->name = variable_get('anonymous', t('Anonymous'));
    }
    $comment->timestamp = !empty($edit['timestamp']) ? $edit['timestamp'] : time();
    $output .= theme('comment_view', $comment, $node);
  }
  $form['comment_preview'] = array(
    '#value' => $output,
    '#weight' => -100,
    '#prefix' => '<div class="preview">',
    '#suffix' => '</div>',
  );
  $output = '';
  if ($edit['pid']) {
    $comment = db_fetch_object(db_query('SELECT c.*, u.uid, u.name AS registered_name, u.signature, u.signature_format, u.picture, u.data FROM {comments} c INNER JOIN {users} u ON c.uid = u.uid WHERE c.cid = %d AND c.status = %d', $edit['pid'], COMMENT_PUBLISHED));
    $comment = drupal_unpack($comment);
    $comment->name = $comment->uid ? $comment->registered_name : $comment->name;
    $output .= theme('comment_view', $comment, $node);
  }
  else {
    $suffix = empty($form['#suffix']) ? '' : $form['#suffix'];
    $form['#suffix'] = $suffix . node_view($node);
    $edit['pid'] = 0;
  }
  $form['comment_preview_below'] = array(
    '#value' => $output,
    '#weight' => 100,
  );
  return $form;
}