template_preprocess_comment

template_preprocess_comment(&$variables)

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 621
Enables users to comment on published content.

Code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
function template_preprocess_comment(&$variables) {
  /** @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 = array(
    '#theme' => 'username',
    '#account' => $account,
  );
  $variables['author'] = drupal_render($username);
  $variables['author_id'] = $comment->getOwnerId();
  $variables['new_indicator_timestamp'] = $comment->getChangedTime();
  $variables['created'] = format_date($comment->getCreatedTime());
  // Avoid calling format_date() twice on the same timestamp.
  if ($comment->getChangedTime() == $comment->getCreatedTime()) {
    $variables['changed'] = $variables['created'];
  }
  else {
    $variables['changed'] = format_date($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'] = user_view($account, 'compact');
  }
  else {
    $variables['user_picture'] = array();
  }
 
  if (isset($comment->in_preview)) {
    $variables['title'] = \Drupal::l($comment->getSubject(), new Url('<front>'));
    $variables['permalink'] = \Drupal::l(t('Permalink'), new Url('<front>'));
  }
  else {
    $uri = $comment->permalink();
    $attributes = $uri->getOption('attributes') ? : array();
    $attributes += array('class' => array('permalink'), 'rel' => 'bookmark');
    $uri->setOption('attributes', $attributes);
    $variables['title'] = \Drupal::l($comment->getSubject(), $uri);
 
    $variables['permalink'] = \Drupal::l(t('Permalink'), $comment->permalink());
  }
 
  $variables['submitted'] = t('Submitted by @username on @datetime', array('@username' => $variables['author'], '@datetime' => $variables['created']));
 
  if ($comment->hasParentComment()) {
    // Fetch and store the parent comment information for use in templates.
    $comment_parent = $comment->getParentComment();
    $account_parent = $comment_parent->getOwner();
    $variables['parent_comment'] = $comment_parent;
    $username = array(
      '#theme' => 'username',
      '#account' => $account_parent,
    );
    $variables['parent_author'] = drupal_render($username);
    $variables['parent_created'] = format_date($comment_parent->getCreatedTime());
    // Avoid calling format_date() twice on the same timestamp.
    if ($comment_parent->getChangedTime() == $comment_parent->getCreatedTime()) {
      $variables['parent_changed'] = $variables['parent_created'];
    }
    else {
      $variables['parent_changed'] = format_date($comment_parent->getChangedTime());
    }
    $permalink_uri_parent = $comment_parent->permalink();
    $attributes = $permalink_uri_parent->getOption('attributes') ? : array();
    $attributes += array('class' => array('permalink'), 'rel' => 'bookmark');
    $permalink_uri_parent->setOption('attributes', $attributes);
    $variables['parent_title'] = \Drupal::l($comment_parent->getSubject(), $permalink_uri_parent);
    $variables['parent_permalink'] = \Drupal::l(t('Parent permalink'), $permalink_uri_parent);
    $variables['parent'] = t('In reply to @parent_title by @parent_username',
    array('@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();
}
doc_Drupal
2025-01-10 15:47:30
Comments
Leave a Comment

Please login to continue.