function comment_preview in Zircon Profile 8
Same name and namespace in other branches
- 8.0 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_render().
1 call to comment_preview()
- CommentForm::preview in core/
modules/ comment/ src/ CommentForm.php - Form submission handler for the 'preview' action.
4 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/ migration_templates/ d6_comment_field_instance.yml - core/modules/comment/migration_templates/d6_comment_field_instance.yml
File
- core/
modules/ comment/ comment.module, line 558 - 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 = 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 = 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;
}