// Only attempt to render comments if the entity has visible comments.
// Unpublished comments are not included in
// $entity->get($field_name)->comment_count, but unpublished comments
// should display if the user is an administrator.
$elements['#cache'
]['contexts'
][] = 'user.permissions';
if ($this->currentUser->
hasPermission('access comments'
) ||
$this->currentUser->
hasPermission('administer comments'
)) { $output['comments'
] =
[];
if ($entity->
get($field_name)->comment_count ||
$this->currentUser->
hasPermission('administer comments'
)) { $mode =
$comment_settings['default_mode'
];
$comments_per_page =
$comment_settings['per_page'
];
$comments =
$this->storage->
loadThread($entity,
$field_name,
$mode,
$comments_per_page,
$this->
getSetting('pager_id'
));
if ($comments) { $build =
$this->viewBuilder->
viewMultiple($comments,
$this->
getSetting('view_mode'
));
$build['pager'
]['#type'
] = 'pager';
// CommentController::commentPermalink() calculates the page number
// where a specific comment appears and does a subrequest pointing to
// that page, we need to pass that subrequest route to our pager to
// keep the pager working.
$build['pager'
]['#route_name'
] =
$this->routeMatch->
getRouteName();
$build['pager'
]['#route_parameters'
] =
$this->routeMatch->
getRawParameters()->
all();
if ($this->
getSetting('pager_id'
)) { $build['pager'
]['#element'
] =
$this->
getSetting('pager_id'
);
}