comment_preview(CommentInterface $comment, FormStateInterface $form_state)
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_render().
File
- core/modules/comment/comment.module, line 561
- Enables users to comment on published content.
Code
function comment_preview(CommentInterface $comment, FormStateInterface $form_state) { $preview_build = array(); $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 = array(); $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 entity_view(). 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 = entity_view($entity, 'full'); } $preview_build['comment_output_below'] = $build; $preview_build['comment_output_below']['#weight'] = 100; return $preview_build; }
Please login to continue.