function comment_preview

Generates a comment preview.

Parameters

Drupal\comment\Comment $comment:

1 call to comment_preview()
CommentFormController::preview in drupal/core/modules/comment/lib/Drupal/comment/CommentFormController.php
Form submission handler for the 'preview' action.
2 string references to 'comment_preview'
CommentTestBase::setCommentPreview in drupal/core/modules/comment/lib/Drupal/comment/Tests/CommentTestBase.php
Sets the value governing the previewing mode for the comment form.
comment_node_type_delete in drupal/core/modules/comment/comment.module
Implements hook_node_type_delete().

File

drupal/core/modules/comment/comment.module, line 1451
Enables users to comment on published content.

Code

function comment_preview(Comment $comment) {
  global $user;
  $preview_build = array();
  if (!form_get_errors()) {

    // Attach the user and time information.
    if (!empty($comment->name->value)) {
      $account = user_load_by_name($comment->name->value);
    }
    elseif ($user->uid && empty($comment->is_anonymous)) {
      $account = $user;
    }
    if (!empty($account->uid)) {
      $comment->uid->target_id = $account->uid;
      $comment->name->value = check_plain($account->name);
    }
    elseif (empty($comment->name->value)) {
      $comment->name->value = config('user.settings')
        ->get('anonymous');
    }
    $comment->created->value = !empty($comment->created->value) ? $comment->created->value : REQUEST_TIME;
    $comment->changed->value = REQUEST_TIME;
    $comment->in_preview = TRUE;
    $comment_build = comment_view($comment);
    $comment_build['#weight'] = -100;
    $preview_build['comment_preview'] = $comment_build;
  }
  if ($comment->pid->target_id) {
    $build = array();
    $comment = $comment->pid->entity;
    if ($comment && $comment->status->value == COMMENT_PUBLISHED) {
      $build = comment_view($comment);
    }
  }
  else {
    $build = node_view($comment->nid->entity);
  }
  $preview_build['comment_output_below'] = $build;
  $preview_build['comment_output_below']['#weight'] = 100;
  return $preview_build;
}