function social_comment_preprocess_comment in Open Social 8
Same name and namespace in other branches
- 8.9 modules/social_features/social_comment/social_comment.module \social_comment_preprocess_comment()
- 8.2 modules/social_features/social_comment/social_comment.module \social_comment_preprocess_comment()
- 8.3 modules/social_features/social_comment/social_comment.module \social_comment_preprocess_comment()
- 8.4 modules/social_features/social_comment/social_comment.module \social_comment_preprocess_comment()
- 8.5 modules/social_features/social_comment/social_comment.module \social_comment_preprocess_comment()
- 8.6 modules/social_features/social_comment/social_comment.module \social_comment_preprocess_comment()
- 8.7 modules/social_features/social_comment/social_comment.module \social_comment_preprocess_comment()
- 8.8 modules/social_features/social_comment/social_comment.module \social_comment_preprocess_comment()
- 10.3.x modules/social_features/social_comment/social_comment.module \social_comment_preprocess_comment()
- 10.0.x modules/social_features/social_comment/social_comment.module \social_comment_preprocess_comment()
- 10.1.x modules/social_features/social_comment/social_comment.module \social_comment_preprocess_comment()
- 10.2.x modules/social_features/social_comment/social_comment.module \social_comment_preprocess_comment()
Implements hook_preprocess_comment().
File
- modules/
social_features/ social_comment/ social_comment.module, line 128 - The Social comment module.
Code
function social_comment_preprocess_comment(&$variables) {
$comment = $variables['elements']['#comment'];
// Display comment created date in format 'time ago'.
$created_time_ago = \Drupal::service('date.formatter')
->formatTimeDiffSince($comment
->getCreatedTime(), [
'granularity' => 1,
'return_as_object' => TRUE,
]);
$date = t('%time ago', [
'%time' => $created_time_ago
->getString(),
]);
$variables['submitted'] = Link::fromTextAndUrl($date, $comment
->urlInfo('canonical'));
$variables['#cache']['max-age'] = $created_time_ago
->getMaxAge();
$account = $comment
->getOwner();
if ($account) {
$storage = \Drupal::entityTypeManager()
->getStorage('profile');
if (!empty($storage)) {
$user_profile = $storage
->loadByUser($account, 'profile');
if ($user_profile) {
$content = \Drupal::entityTypeManager()
->getViewBuilder('profile')
->view($user_profile, 'compact');
$variables['author_picture'] = $content;
}
}
}
// Add node ID attribute for comment "new" indicator.
if (\Drupal::moduleHandler()
->moduleExists('history') && $comment
->getCommentedEntityTypeId() == 'node') {
$variables['attributes']['data-history-node-id'] = $comment
->getCommentedEntityId();
}
}