function 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 node objects. Array keys: #comment, #node.

File

drupal/core/modules/comment/comment.module, line 1535
Enables users to comment on published content.

Code

function template_preprocess_comment(&$variables) {
  $comment = $variables['elements']['#comment'];
  $node = $variables['elements']['#node'];
  $variables['comment'] = $comment;
  $variables['node'] = $node;
  $account = comment_prepare_author($comment);

  // @todo Do not call theme() here. We do this for purposes of t().
  $variables['author'] = theme('username', array(
    'account' => $account,
  ));
  $variables['new'] = $comment->new->value ? t('new') : '';
  $variables['created'] = format_date($comment->created->value);

  // Avoid calling format_date() twice on the same timestamp.
  if ($comment->changed->value == $comment->created->value) {
    $variables['changed'] = $variables['created'];
  }
  else {
    $variables['changed'] = format_date($comment->changed->value);
  }
  if (theme_get_setting('features.comment_user_picture')) {

    // To change user picture settings (e.g., image style), edit the 'compact'
    // view mode on the User entity.
    $variables['user_picture'] = user_view($account, 'compact');
  }
  else {
    $variables['user_picture'] = array();
  }
  if (config('user.settings')
    ->get('signatures') && !empty($account->signature)) {
    $variables['signature'] = check_markup($account->signature, $account->signature_format, '', TRUE);
  }
  else {
    $variables['signature'] = '';
  }
  $uri = $comment
    ->uri();
  $permalink_uri = $comment
    ->permalink();
  $uri['options'] += array(
    'attributes' => array(
      'class' => 'permalink',
      'rel' => 'bookmark',
    ),
  );
  $variables['title'] = l($comment->subject->value, $uri['path'], $uri['options']);
  $variables['permalink'] = l(t('Permalink'), $permalink_uri['path'], $permalink_uri['options']);
  $variables['submitted'] = t('Submitted by !username on !datetime', array(
    '!username' => $variables['author'],
    '!datetime' => $variables['created'],
  ));
  if ($comment->pid->target_id) {

    // Fetch and store the parent comment information for use in templates.
    $comment_parent = $comment->pid->entity;
    $account_parent = comment_prepare_author($comment_parent);
    $variables['parent_comment'] = $comment_parent;

    // @todo Do not call theme() here. We do this for purposes of t().
    $variables['parent_author'] = theme('username', array(
      'account' => $account_parent,
    ));
    $variables['parent_created'] = format_date($comment_parent->created->value);

    // Avoid calling format_date() twice on the same timestamp.
    if ($comment_parent->changed->value == $comment_parent->created->value) {
      $variables['parent_changed'] = $variables['parent_created'];
    }
    else {
      $variables['parent_changed'] = format_date($comment_parent->changed->value);
    }
    $permalink_uri_parent = $comment_parent
      ->permalink();
    $permalink_uri_parent['options'] += array(
      'attributes' => array(
        'class' => array(
          'permalink',
        ),
        'rel' => 'bookmark',
      ),
    );
    $variables['parent_title'] = l($comment_parent->subject->value, $permalink_uri_parent['path'], $permalink_uri_parent['options']);
    $variables['parent_permalink'] = l(t('Parent permalink'), $permalink_uri_parent['path'], $permalink_uri_parent['options']);
    $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'] = '';
  }

  // Preprocess fields.
  field_attach_preprocess($comment, $variables['elements'], $variables);

  // 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->status->value == COMMENT_NOT_PUBLISHED ? 'unpublished' : 'published';
  }

  // Gather comment classes.
  $variables['attributes']['class'][] = 'comment';

  // 'published' class is not needed, it is either 'preview' or 'unpublished'.
  if ($variables['status'] != 'published') {
    $variables['attributes']['class'][] = $variables['status'];
  }
  if ($variables['new']) {
    $variables['attributes']['class'][] = 'new';
  }
  if (!$comment->uid->target_id) {
    $variables['attributes']['class'][] = 'by-anonymous';
  }
  else {
    if ($comment->uid->target_id == $variables['node']->uid) {
      $variables['attributes']['class'][] = 'by-node-author';
    }
    if ($comment->uid->target_id == $variables['user']->uid) {
      $variables['attributes']['class'][] = 'by-viewer';
    }
  }

  // Add clearfix class.
  $variables['attributes']['class'][] = 'clearfix';
  $variables['content_attributes']['class'][] = 'content';
}