6

I'm trying to set a specific style for user pictures in comments in drupal 7, but can't find how to do it. I know there is a module for this (http://drupal.org/project/imagecache_profiles) but it's really an overkill since I just want pictures to be themed in comments, according to a style I already created in Image_cache configuration.

In comment.tpl.php, there is a <?php print $picture; ?> , but it's directly the html for the image, so I can't apply the theme function of image cache, for instance theme('imagecache','size32x32', $data->users_picture); The $author variable is also html.

Is there a way to get the theme function to work in this comment.tpl.php file ? Is there another way to achieve this ? Thanks !

5

To help you understand the syntax, have a look at the comment.tpl.php documentation, here you will find the different variables and a link to template_preprocess_comment() where they are defined:

$variables['picture']   = theme_get_setting('toggle_comment_user_picture') ? 
                          theme('user_picture', array('account' => $comment)) : '';

So there's a theme function to render this picture: theme_user_picture() which state you should have a look a the template file: user-picture.tpl.php which finally send you to template_preprocess_user_picture().

There you can override this function copying and pasting the original code and changing the bits and parts you want to suit your needs (put this code in your template.php file):

function YOURTHEME_preprocess_user_picture(&$variables) {
  //change only the relevant part and leave the rest of the code
  //For Drupal 7 imagecache is in core so I put the correct syntax
  $variables['user_picture'] = theme('image_style', array('style_name' => 'size32x32', 'path' => $filepath, 'alt' => $alt, 'title' => $alt));

  //...
}

Have a look at the $variables to find a way to target comments or add a class there and use CSS as dkiscool mentionned.

NB: if you don't like browsing all the API documentation, there's a faster way, use theme developer (see this answer).

  • Thanks for this awesome answer. I don't follow how exactly you jump from file to file until this _preprocess_ser_picture, but I'm definitely going to try to understand it, to replicate it, and to try this "theme developer" tool. So thanks again for dateiling how you got this answer. – fourmi4x Jan 8 '12 at 22:33
  • 1
    Actually, I needed to add a few variables to get it to work: function mytheme_preprocess_user_picture(&$variables) { $account = $variables['account']; if (!empty($account->picture->uri)) { $filepath = $account->picture->uri; } if (isset($filepath)) { $alt = t("@user's picture", array('@user' => format_username($account))); $variables['user_picture'] = theme('image_style', array('style_name' => 'size32x32', 'path' => $filepath, 'alt' => $alt, 'title' => $alt)); } } Thanks again ! – fourmi4x Jan 8 '12 at 22:35
1

In your theme's template.php

function yourtheme_image($variables) {
        $attributes = $variables['attributes'];
        $attributes['src'] = file_create_url($variables['path']);

        foreach (array('width', 'height', 'alt', 'title') as $key) {

            if (isset($variables[$key])) {
                $attributes[$key] = $variables[$key];
            }
        }

        return '<div class="your_class"><img' . drupal_attributes($attributes) . ' /></div>';
}

this will place a "your_class" around all the images on your site then in your css file you can target it as

. comment  .your_class {border: solid 1px red;}

so that it targets only images on comments

  • Hi, Thanks for this interesting snippet, however I wanted to modify it not with css but directly at the output. But I'llo make use of your code somewhere else... – fourmi4x Jan 8 '12 at 22:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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