comment_form_add_preview

Versions
4.7 – 5
comment_form_add_preview($form, $edit)
6
comment_form_add_preview($form, &$form_state)

Code

modules/comment.module, line 1362

<?php
function comment_form_add_preview($form, $edit) {
  global $user;

  drupal_set_title(t('Preview comment'));

  $output = '';

  // 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);
  if (!form_get_errors()) {
    $comment = (object)_comment_form_submit($edit);

    // Attach the user and time information.
    if ($edit['author']) {
      $account = user_load(array('name' => $edit['author']));
    }
    elseif ($user->uid && !isset($edit['is_anonymous'])) {
      $account = $user;
    }
    if ($account) {
      $comment->uid = $account->uid;
      $comment->name = check_plain($account->name);
    }
    $comment->timestamp = $edit['timestamp'] ? $edit['timestamp'] : time();
    $output .= theme('comment_view', $comment);
  }
  $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.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);
  }
  else {
    $form['#suffix'] = node_view(node_load($edit['nid']));
    $edit['pid'] = 0;
  }

  $form['comment_preview_below'] = array('#value' => $output, '#weight' => 100);

  return $form;
}
?>
Login or register to post comments
 
 

All source code and documentation on this site is released under the terms of the GNU General Public License, version 2 and later. Drupal is a registered trademark of Dries Buytaert.