Same name and namespace in other branches
- 4.6.x modules/comment.module \comment_preview()
- 7.x modules/comment/comment.module \comment_preview()
- 8.9.x core/modules/comment/comment.module \comment_preview()
- 9 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().
9 string references to 'comment_preview'
- CommentType::getCommentFields in core/
modules/ comment/ src/ Plugin/ migrate/ source/ CommentType.php - Returns the fields containing comment settings for each node type.
- CommentTypeTest::providerSource in core/
modules/ comment/ tests/ src/ Kernel/ Plugin/ migrate/ source/ CommentTypeTest.php - 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
- MigrateCommentTypeTest::testMigration in core/
modules/ comment/ tests/ src/ Kernel/ Migrate/ d7/ MigrateCommentTypeTest.php - Tests the migrated comment types.
File
- core/
modules/ comment/ comment.module, line 505 - Enables users to comment on published content.
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;
}