8.5.x comment.module template_preprocess_comment(&$variables)
8.0.x comment.module template_preprocess_comment(&$variables)
8.1.x comment.module template_preprocess_comment(&$variables)
8.2.x comment.module template_preprocess_comment(&$variables)
8.3.x comment.module template_preprocess_comment(&$variables)
8.4.x comment.module template_preprocess_comment(&$variables)
8.6.x comment.module template_preprocess_comment(&$variables)
6.x comment.module template_preprocess_comment(&$variables)
7.x comment.module template_preprocess_comment(&$variables)

Process variables for comment.tpl.php.

See also

comment.tpl.php

File

modules/comment/comment.module, line 2289
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;
  $variables['author'] = theme('username', array(
    'account' => $comment,
  ));
  $variables['created'] = format_date($comment->created);

  // Avoid calling format_date() twice on the same timestamp.
  if ($comment->changed == $comment->created) {
    $variables['changed'] = $variables['created'];
  }
  else {
    $variables['changed'] = format_date($comment->changed);
  }
  $variables['new'] = !empty($comment->new) ? t('new') : '';
  $variables['picture'] = theme_get_setting('toggle_comment_user_picture') ? theme('user_picture', array(
    'account' => $comment,
  )) : '';
  $variables['signature'] = $comment->signature;
  $uri = entity_uri('comment', $comment);
  $uri['options'] += array(
    'attributes' => array(
      'class' => array(
        'permalink',
      ),
      'rel' => 'bookmark',
    ),
  );
  $variables['title'] = l($comment->subject, $uri['path'], $uri['options']);
  $variables['permalink'] = l(t('Permalink'), $uri['path'], $uri['options']);
  $variables['submitted'] = t('Submitted by !username on !datetime', array(
    '!username' => $variables['author'],
    '!datetime' => $variables['created'],
  ));

  // Preprocess fields.
  field_attach_preprocess('comment', $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'] = 'comment-preview';
  }
  else {
    $variables['status'] = $comment->status == COMMENT_NOT_PUBLISHED ? 'comment-unpublished' : 'comment-published';
  }

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

Comments

BarisW’s picture

A small snippet to change the Permalink to display #1 instead:

<?php
/**
 * Implements hook_preprocess_comment().
 */
function MYTHEME_preprocess_comment(&$vars) {
  // Change the Permalink to display #1 instead of 'Permalink'
  $comment = $vars['comment'];
  $uri = entity_uri('comment', $comment);
  $uri['options'] += array('attributes' => array(
    'class' => 'permalink',
    'rel' => 'bookmark',
  ));
  $vars['permalink'] = l('#' . $vars['id'], $uri['path'], $uri['options']);
}
?>
bosmaen’s picture

Thanks a lot BarisW

reyna_cortez’s picture

I would like to edit this, I hope I can help in drupal 8, $variables['submitted'] = t('Submitted by !username on !datetime', array('!username' => $variables['author'], '!datetime' => $variables['created']));