public function SocialCommentLazyRenderer::renderComments in Open Social 10.1.x
Same name and namespace in other branches
- 8.9 modules/social_features/social_comment/src/SocialCommentLazyRenderer.php \Drupal\social_comment\SocialCommentLazyRenderer::renderComments()
- 10.3.x modules/social_features/social_comment/src/SocialCommentLazyRenderer.php \Drupal\social_comment\SocialCommentLazyRenderer::renderComments()
- 10.0.x modules/social_features/social_comment/src/SocialCommentLazyRenderer.php \Drupal\social_comment\SocialCommentLazyRenderer::renderComments()
- 10.2.x modules/social_features/social_comment/src/SocialCommentLazyRenderer.php \Drupal\social_comment\SocialCommentLazyRenderer::renderComments()
Render comments for lazy builder.
Parameters
string $entity_type: The entity type.
string|int $entity_id: The entity id.
string $view_mode: The view mode from field settings.
string $field_name: The field name.
string|int|null $num_comments: The number of comments.
int $pager_id: Pager id to use in case of multiple pagers on the one page.
string $build_view_mode: The build view mode.
Return value
mixed The render array.
Throws
\Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException
\Drupal\Component\Plugin\Exception\PluginNotFoundException
File
- modules/
social_features/ social_comment/ src/ SocialCommentLazyRenderer.php, line 86
Class
- SocialCommentLazyRenderer
- Render comments for the lazy builder.
Namespace
Drupal\social_commentCode
public function renderComments($entity_type, $entity_id, $view_mode, $field_name, $num_comments, $pager_id, $build_view_mode = 'default') {
/** @var \Drupal\Core\Entity\EntityInterface $entity */
$entity = $this->entityTypeManager
->getStorage($entity_type)
->load($entity_id);
/** @var \Drupal\comment\CommentInterface[] $comments */
$comments = $this->entityTypeManager
->getStorage('comment')
->loadThread($entity, $field_name, $view_mode, $num_comments, $pager_id);
if (!$comments) {
return [];
}
$build_comments = $this->entityTypeManager
->getViewBuilder('comment')
->viewMultiple($comments, $build_view_mode);
if ($build_comments) {
$build_comments['pager']['#type'] = 'pager';
$build_comments['pager']['#route_name'] = $this->routeMatch
->getRouteObject();
$build_comments['pager']['#route_parameters'] = $this->routeMatch
->getRawParameters()
->all();
if ($pager_id) {
$build_comments['pager']['#element'] = $pager_id;
}
}
// Since we are rendering it as lazy builder, make sure we attach classes
// required by ajax_comments. In order to render reply forms etc.
if (!empty($build_comments) && $this->moduleHandler
->moduleExists('ajax_comments')) {
Utility::addCommentClasses($build_comments);
}
return $build_comments;
}