function template_preprocess_comment in Drupal 9
Same name and namespace in other branches
- 8 core/modules/comment/comment.module \template_preprocess_comment()
- 6 modules/comment/comment.module \template_preprocess_comment()
- 7 modules/comment/comment.module \template_preprocess_comment()
- 10 core/modules/comment/comment.module \template_preprocess_comment()
Prepares variables for comment templates.
Default template: comment.html.twig.
Parameters
array $variables: An associative array containing:
- elements: An associative array containing the comment and entity objects. Array keys: #comment, #commented_entity.
File
- core/
modules/ comment/ comment.module, line 556 - Enables users to comment on published content.
Code
function template_preprocess_comment(&$variables) {
/** @var \Drupal\Core\Datetime\DateFormatterInterface $date_formatter */
$date_formatter = \Drupal::service('date.formatter');
/** @var \Drupal\comment\CommentInterface $comment */
$comment = $variables['elements']['#comment'];
$commented_entity = $comment
->getCommentedEntity();
$variables['comment'] = $comment;
$variables['commented_entity'] = $commented_entity;
$variables['threaded'] = $variables['elements']['#comment_threaded'];
$account = $comment
->getOwner();
$username = [
'#theme' => 'username',
'#account' => $account,
];
$variables['author'] = \Drupal::service('renderer')
->render($username);
$variables['author_id'] = $comment
->getOwnerId();
$variables['new_indicator_timestamp'] = $comment
->getChangedTime();
$variables['created'] = $date_formatter
->format($comment
->getCreatedTime());
// Avoid calling DateFormatterInterface::format() twice on the same timestamp.
if ($comment
->getChangedTime() == $comment
->getCreatedTime()) {
$variables['changed'] = $variables['created'];
}
else {
$variables['changed'] = $date_formatter
->format($comment
->getChangedTime());
}
if (theme_get_setting('features.comment_user_picture')) {
// To change user picture settings (for instance, image style), edit the
// 'compact' view mode on the User entity.
$variables['user_picture'] = \Drupal::entityTypeManager()
->getViewBuilder('user')
->view($account, 'compact');
}
else {
$variables['user_picture'] = [];
}
if (isset($comment->in_preview)) {
$variables['title'] = Link::fromTextAndUrl($comment
->getSubject(), Url::fromRoute('<front>'))
->toString();
$variables['permalink'] = Link::fromTextAndUrl(t('Permalink'), Url::fromRoute('<front>'))
->toString();
}
else {
$uri = $comment
->permalink();
$attributes = $uri
->getOption('attributes') ?: [];
$attributes += [
'class' => [
'permalink',
],
'rel' => 'bookmark',
];
$uri
->setOption('attributes', $attributes);
$variables['title'] = Link::fromTextAndUrl($comment
->getSubject(), $uri)
->toString();
$variables['permalink'] = Link::fromTextAndUrl(t('Permalink'), $comment
->permalink())
->toString();
}
$variables['submitted'] = t('Submitted by @username on @datetime', [
'@username' => $variables['author'],
'@datetime' => $variables['created'],
]);
if ($comment_parent = $comment
->getParentComment()) {
// Fetch and store the parent comment information for use in templates.
$account_parent = $comment_parent
->getOwner();
$variables['parent_comment'] = $comment_parent;
$username = [
'#theme' => 'username',
'#account' => $account_parent,
];
$variables['parent_author'] = \Drupal::service('renderer')
->render($username);
$variables['parent_created'] = $date_formatter
->format($comment_parent
->getCreatedTime());
// Avoid calling DateFormatterInterface::format() twice on same timestamp.
if ($comment_parent
->getChangedTime() == $comment_parent
->getCreatedTime()) {
$variables['parent_changed'] = $variables['parent_created'];
}
else {
$variables['parent_changed'] = $date_formatter
->format($comment_parent
->getChangedTime());
}
$permalink_uri_parent = $comment_parent
->permalink();
$attributes = $permalink_uri_parent
->getOption('attributes') ?: [];
$attributes += [
'class' => [
'permalink',
],
'rel' => 'bookmark',
];
$permalink_uri_parent
->setOption('attributes', $attributes);
$variables['parent_title'] = Link::fromTextAndUrl($comment_parent
->getSubject(), $permalink_uri_parent)
->toString();
$variables['parent_permalink'] = Link::fromTextAndUrl(t('Parent permalink'), $permalink_uri_parent)
->toString();
$variables['parent'] = t('In reply to @parent_title by @parent_username', [
'@parent_username' => $variables['parent_author'],
'@parent_title' => $variables['parent_title'],
]);
}
else {
$variables['parent_comment'] = '';
$variables['parent_author'] = '';
$variables['parent_created'] = '';
$variables['parent_changed'] = '';
$variables['parent_title'] = '';
$variables['parent_permalink'] = '';
$variables['parent'] = '';
}
// Helpful $content variable for templates.
foreach (Element::children($variables['elements']) as $key) {
$variables['content'][$key] = $variables['elements'][$key];
}
// Set status to a string representation of comment->status.
if (isset($comment->in_preview)) {
$variables['status'] = 'preview';
}
else {
$variables['status'] = $comment
->isPublished() ? 'published' : 'unpublished';
}
// Add comment author user ID. Necessary for the comment-by-viewer library.
$variables['attributes']['data-comment-user-id'] = $comment
->getOwnerId();
// Add anchor for each comment.
$variables['attributes']['id'] = 'comment-' . $comment
->id();
}