You are here

function social_comment_preprocess_comment in Open Social 8

Same name and namespace in other branches
  1. 8.9 modules/social_features/social_comment/social_comment.module \social_comment_preprocess_comment()
  2. 8.2 modules/social_features/social_comment/social_comment.module \social_comment_preprocess_comment()
  3. 8.3 modules/social_features/social_comment/social_comment.module \social_comment_preprocess_comment()
  4. 8.4 modules/social_features/social_comment/social_comment.module \social_comment_preprocess_comment()
  5. 8.5 modules/social_features/social_comment/social_comment.module \social_comment_preprocess_comment()
  6. 8.6 modules/social_features/social_comment/social_comment.module \social_comment_preprocess_comment()
  7. 8.7 modules/social_features/social_comment/social_comment.module \social_comment_preprocess_comment()
  8. 8.8 modules/social_features/social_comment/social_comment.module \social_comment_preprocess_comment()
  9. 10.3.x modules/social_features/social_comment/social_comment.module \social_comment_preprocess_comment()
  10. 10.0.x modules/social_features/social_comment/social_comment.module \social_comment_preprocess_comment()
  11. 10.1.x modules/social_features/social_comment/social_comment.module \social_comment_preprocess_comment()
  12. 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();
  }
}