Specifically, every time a comment is saved, I'd like to programatically change a comment field 'Picture' to an image field from the user who wrote the comment. I have the following custom module:

function MYMODULE_comment_presave($comment) {
    $user = user_load($comment->uid);
    $uid = $user->uid;

    $comment->field_picture['und'][0]['fid'] = $user->field_picture['und'][0]['fid'];

This doesn't work correctly, because the field_picture_alt, field_picture_width, field_picture_title and field_picture_height columns in my database don't get a value (they're NULL).

I've also tried

function MYMODULE_comment_presave($comment) {
    $user = user_load($comment->uid);
    $uid = $user->uid;

    $comment->field_picture['und'][0]['fid'] = $user->field_picture['und'][0]['fid'];
    $comment->field_picture['und'][0]['width'] = $user->field_picture['und'][0]['width'];
    $comment->field_picture['und'][0]['height'] = $user->field_picture['und'][0]['height'];
    $comment->field_picture['und'][0]['alt'] = $user->field_picture['und'][0]['alt'];
    $comment->field_picture['und'][0]['title'] = $user->field_picture['und'][0]['title'];

and that works perfectly.

However, I was wondering whether I always have to set the 'alt', 'title', 'width' and 'height' values manually as above.

Is there another faster way to inherit these caracteristics from the referred image? Can I for example inherit the 'height' and 'width' from an image style?

  • For nodes, drupal.stackexchange.com/questions/101728/… shows a way to do it; perhaps it can be modified to work with comments
    – Jimajamma
    Feb 20, 2014 at 19:32
  • @Jimajamma: Yeah, that was one of my previous questions. However, in this case it seems unnecessary to me to save the image again, because it's already in the database. I can't imagine that it's impossible to use it elsewhere.
    – Jeroen
    Feb 20, 2014 at 20:11

1 Answer 1


You can do this in the comment tpl file. Add something like this:

$img_array = array(
    'style_name' => 'thumbnail',
    'path' => $user->field_picture['und'][0]['uri'],
    'width' => '',
    'height' => '',
    'alt' => $user->field_picture['und'][0]['alt'],
    'title' => $user->field_picture['und'][0]['title'],

$img = theme('image_style', $img_array);

You can then add <?php print $img; ?> where you want the image to appear.

  • Since files are stored in the server file system in addition to the db, they work somewhat differently from other fields. You can read about the drupal file api here: drupal.org/documentation/modules/file
    – Ollie
    Feb 21, 2014 at 20:11

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.