function comment_preview

Same name in other branches
  1. 7.x modules/comment/comment.module \comment_preview()
  2. 9 core/modules/comment/comment.module \comment_preview()
  3. 8.9.x core/modules/comment/comment.module \comment_preview()
  4. 11.x core/modules/comment/comment.module \comment_preview()

Generates a comment preview.

Parameters

\Drupal\comment\CommentInterface $comment: The comment entity to preview.

Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.

Return value

array An array as expected by \Drupal\Core\Render\RendererInterface::render().

1 call to comment_preview()
CommentForm::preview in core/modules/comment/src/CommentForm.php
Form submission handler for the 'preview' action.
5 string references to 'comment_preview'
CommentForm::actions in core/modules/comment/src/CommentForm.php
Returns an array of supported actions for the current entity form.
CommentForm::form in core/modules/comment/src/CommentForm.php
Gets the actual form array to be built.
CommentForm::preview in core/modules/comment/src/CommentForm.php
Form submission handler for the 'preview' action.
d6_comment_field_instance.yml in core/modules/comment/migrations/d6_comment_field_instance.yml
core/modules/comment/migrations/d6_comment_field_instance.yml
d7_comment_field_instance.yml in core/modules/comment/migrations/d7_comment_field_instance.yml
core/modules/comment/migrations/d7_comment_field_instance.yml

File

core/modules/comment/comment.module, line 505

Code

function comment_preview(CommentInterface $comment, FormStateInterface $form_state) {
    $preview_build = [];
    $entity = $comment->getCommentedEntity();
    if (!$form_state->getErrors()) {
        $comment->in_preview = TRUE;
        $comment_build = \Drupal::entityTypeManager()->getViewBuilder('comment')
            ->view($comment);
        $comment_build['#weight'] = -100;
        $preview_build['comment_preview'] = $comment_build;
    }
    if ($comment->hasParentComment()) {
        $build = [];
        $parent = $comment->getParentComment();
        if ($parent && $parent->isPublished()) {
            $build = \Drupal::entityTypeManager()->getViewBuilder('comment')
                ->view($parent);
        }
    }
    else {
        // The comment field output includes rendering the parent entity of the
        // thread to which the comment is a reply. The rendered entity output
        // includes the comment reply form, which contains the comment preview and
        // therefore the rendered parent entity. This results in an infinite loop of
        // parent entity output rendering the comment form and the comment form
        // rendering the parent entity. To prevent this infinite loop we temporarily
        // set the value of the comment field on a clone of the entity to hidden
        // before calling the entity view builder. That way when the output of
        // the commented entity is rendered, it excludes the comment field output.
        $field_name = $comment->getFieldName();
        $entity = clone $entity;
        $entity->{$field_name}->status = CommentItemInterface::HIDDEN;
        $build = \Drupal::entityTypeManager()->getViewBuilder($entity->getEntityTypeId())
            ->view($entity, 'full');
    }
    $preview_build['comment_output_below'] = $build;
    $preview_build['comment_output_below']['#weight'] = 200;
    return $preview_build;
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.