1

I'm trying to print a custom user field (field_avatar) image as a replacement for the user picture. Why? user picture file directory would not accept tokens. This mean, I could run into a scenario where there are 100k files in a directory slowing the website!

From googling answers for hours, this is the best solution I've read.

In comment.tpl.php placed in my custom omega 4 subtheme, I added this code.

<?php  print render(field_view_field('user', $user, 'field_avatar'));  ?>

However, above code prints the default field_avatar picture! not field_avatar based on the comment ID? or UID of the author?

Please shed some wisdom smart ones!

2 Answers 2

2

I've got it working! I've used user_load($comment->uid) to load the author relationship.

I placed the code in a copy of comment.tpl.php in my sub theme.

<?php print render(field_view_field('user',user_load($comment->uid), 'YOUR-CUSTOM-USER-FIELD'));?>

Cheers and good luck!

0

You need to reference the $author of the comment rather than the currently logged in $user. Luckily, $author comes as a parameter to the tpl file, try this?:

<?php
  print render(field_view_field('user', $author, 'field_avatar'));
?>
11
  • I tried it. Cleared the cache and everything. Unfortunately, It's still rendering the default picture. Note: I'm not trying to print the current user of the site, but the author of the comment.
    – Ian
    Commented Sep 7, 2014 at 0:35
  • Oh of course, hang on I'll update the answer.
    – Darvanen
    Commented Sep 7, 2014 at 0:44
  • Unfortunately, I've tried $author and $node to no avail :(. I'm reading comment.module to get some insight.
    – Ian
    Commented Sep 7, 2014 at 0:57
  • perhaps render() doesn't work the same way for $author fields, or the $author object is structured differently to $user. You can use dpm($author); to check the object (make sure devel module is installed first).
    – Darvanen
    Commented Sep 7, 2014 at 1:17
  • I'll just resort to views for now. >.>
    – Ian
    Commented Sep 7, 2014 at 2:21

Your Answer

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

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