comment_form_add_preview

  1. drupal
    1. drupal6
    2. drupal7
Versionen
drupal6 – drupal7 comment_form_add_preview($form, &$form_state)

Form builder; Generate and validate a comment preview form.

Verwandte Themen

Code

modules/comment/comment.module, line 1663

<?php
function comment_form_add_preview($form, &$form_state) {
  global $user;
  $edit = $form_state['values'];
  drupal_set_title(t('Preview comment'), PASS_THROUGH);
  $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 text 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;
    $comment->format = $comment->comment_format;

    // Attach the user and time information.
    if (!empty($edit['author'])) {
      $account = user_load_by_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'] : REQUEST_TIME;
    $output .= theme('comment_view', $comment, $node);
  }

  $form['comment_preview'] = array(
    '#markup' => $output,
    '#weight' => -100,
    '#prefix' => '<div class="preview">',
    '#suffix' => '</div>',
  );

  $output = ''; // Isn't this line a duplication of the first $output above?

  if ($edit['pid']) {
    $comment = db_query('SELECT c.*, u.uid, u.name AS registered_name, u.signature, u.picture, u.data FROM {comment} c INNER JOIN {users} u ON c.uid = u.uid WHERE c.cid = :cid AND c.status = :status', array(
      ':cid' => $edit['pid'],
      ':status' => COMMENT_PUBLISHED,
      ))->fetchObject();
    $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 . drupal_render(node_build($node));
    $edit['pid'] = 0;
  }

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

  return $form;
}
?>

Kommentare

Kommentar hinzufügen

Der Inhalt dieses Feldes wird nicht öffentlich zugänglich angezeigt.
  • Internet- und E-Mail-Adressen werden automatisch umgewandelt.
  • Zulässige HTML-Tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Zeilen und Absätze werden automatisch erzeugt.

Weitere Informationen über Formatierungsoptionen

Kommentar hinzufügen

Der Inhalt dieses Feldes wird nicht öffentlich zugänglich angezeigt.
  • Internet- und E-Mail-Adressen werden automatisch umgewandelt.
  • Zulässige HTML-Tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Zeilen und Absätze werden automatisch erzeugt.

Weitere Informationen über Formatierungsoptionen