0

The below code is from the comment.tpl.php file:

<div class="comment-avartar">
<?php print $picture ?>
<div class="submitted"><?php print render($content['submitted_by'] ['#markup']);?></a></div>

I want "$content['submitted_by'] ['#markup']" to link to the user profile of the user who posted the comment.

i have tried editing the code to something like this:

<?php $link = drupal_get_path_alias('user/' . $user->uid);?>

<div class="comment-avartar">
<?php print $picture ?>
<div class="submitted"><a href="<?php $link ;?>"><?php print render($content['submitted_by'] ['#markup']);?></a></div>

But it links to the user profile of the currently logged in user.

What I would like is to link it to the profile of the Comment author.

Thank you.

1
  • Have you enable in theme settings for "User pictures in comments" to be check marked?
    – vgoradiya
    Jul 7, 2016 at 8:58

1 Answer 1

0

Comment module does add a link to comment author name on comments view. The link is conditionally visible to user's i.e. If user has permission to "View user profiles" or administrative permission then he will be able to see author name as link, else plain text. See screenshots below (tested with Anonymous user with and without permission "View User Profiles"):

Permissions: enter image description here

Comment preview with permission:

enter image description here

Comment preview without permission:

enter image description here

You can still add a link to author name by your own, by implementing template_preprocess_comment() function in your theme's template.php or .module file. This might lead to access denied page:

/**
 * Process variables for comment.tpl.php.
 *
 * @see comment.tpl.php
 */
function [THEME_NAME]_preprocess_comment(&$variables) {
  if (!empty($variables['elements']['#comment']->uid)) {
    $variables['comment_author'] = l($variables['elements']['#comment']->name, 'user/1/' . $variables['elements']['#comment']->uid);
  }
  else {
    $variables['comment_author'] = t('Anonymous');
  }
}

And then printing "$comment_author" variable in comment.tpl.php file, see below.

<p class="commenter-name">
  <?php print $comment_author; ?>
</p>
3
  • Thanks for this. It works as you have outlined. However using your approach, it is the "username" that links to the user profile. In my case, i have edited the "submitted by" value to custom fields (first & last name). That is why i added the <a> tag.
    – seyi
    Jul 7, 2016 at 10:10
  • Glad, it helped...
    – Yogesh
    Jul 7, 2016 at 10:11
  • Thanks for this. It works as you have outlined. However using your approach, it is the "username" that links to the user profile. In my case, i have edited the "submitted by" value to custom fields (first & last name). That is why i added the <a> tag. –
    – seyi
    Jul 7, 2016 at 10:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.