2

I'm trying to add the user picture before the username in user listings. I've copied theme_username into my template.php and I'm trying to call theme with the argument 'user_picture' in order to get the HTML that will render the picture, but the call just returns an empty string. I'm providing the uid as the argument. Where am I going wrong? Thanks.

function MY-THEME_username($variables) {
  if (isset($variables['link_path'])) {
    // We have a link path, so we should generate a link using l().
    // Additional classes may be added as array elements like
    // $variables['link_options']['attributes']['class'][] = 'myclass';

    // I ADDED THIS LINE

    $output = theme('user_picture', array('account' => $variables['account']));

    // ENDS

    $output .= l($variables['name'] . $variables['extra'], $variables['link_path'], $variables['link_options']);
  }
  else {
    // Modules may have added important attributes so they must be included
    // in the output. Additional classes may be added as array elements like
    // $variables['attributes_array']['class'][] = 'myclass';
    $output = '<span' . drupal_attributes($variables['attributes_array']) . '>' . $variables['name'] . $variables['extra'] . '</span>';
  }
  return $output;
}
  • Any reason you don't use Views module for a custom user listing? Either with fields and custom field configuration, or with entity view modes / Display suite.. – donquixote Nov 24 '13 at 18:36
4
+50

theme_user_picture is not a Drupal 7 function, whereas in the example you provide you are using Drupal 7's theme_username function code. Moreover, even in Drupal 6, theme_user_picture returns nothing because in that version of Drupal it's merely a placeholder and the actual themeing of the user entry is handled via the corresponding template file user-picture.tpl.php. This is possibly the reason your theme call returns nothing. Please refer to api.drupal.org to find the correct implementation for your Drupal version.

So, assuming that you are on Drupal 7, note that $variables['account'] is not a fully populated user object and does not contain any reference to the user picture. You need to actually load the user object and give the picture some themeing in order to show it in your listing. Your code could be something like this:

function MY-THEME_username($variables) {
  if (isset($variables['link_path'])) {
    // We have a link path, so we should generate a link using l().
    // Additional classes may be added as array elements like
    // $variables['link_options']['attributes']['class'][] = 'myclass';

    // I ADDED THIS LINE

    //$output = theme('user_picture', array('account' => $variables['account']));
    $user = user_load($variables['account']->uid);
    if (isset($user->picture->uri)) {
      $output = theme('image_style', array('path' => $user->picture->uri,'style_name' => 'thumbnail'));
    }
    // ENDS

    $output .= l($variables['name'] . $variables['extra'], $variables['link_path'], $variables['link_options']);
  }
  else {
    // Modules may have added important attributes so they must be included
    // in the output. Additional classes may be added as array elements like
    // $variables['attributes_array']['class'][] = 'myclass';
    $output = '<span' . drupal_attributes($variables['attributes_array']) . '>' . $variables['name'] . $variables['extra'] . '</span>';
  }
  return $output;
}
2

There's a typo ($ouput vs $output) and incorrect function param (uid vs account) there.

You could try:

$output = theme('user_picture', array('account' => $variables['account']));

Note, user pictures will need to be enabled in user account config settings. ($conf['user_pictures'])

  • Thanks. Made the typo correction and changed the param. User pictures enabled. Flushed all caches. Still returns nothing? – 24ma13wg Nov 22 '13 at 10:55
  • @user1919784 keep in mind your user picture will only show if visitors have access to 'view user profiles' that's because it's only added when $variables['link_path'] is set. You might want to add it after the if/else. – David Thomas Nov 25 '13 at 23:12
  • Thanks, but it doesn't return pictures in a list to user #1. So, I don't think it can be a permissions thing can it? I probably need to look for some more examples to see how other's got it to work - maybe there's something in the examples project. – 24ma13wg Nov 26 '13 at 9:47
0

Instead using Register with Picture module, you can set the weight of the picture field.

Use the Registration Picture Field Weight field to adjust the display order of the picture field on the registration form. It can be a negative or positive number.

This module exposes the 'upload picture' element to the registration form. It also gives you the ability to set a picture as required in the module settings.

  • Thanks, but I'm trying to figure out why my call to theme doesn't return any HTML. – 24ma13wg Nov 24 '13 at 9:46
0

What user listing are you trying create or modify? If it is the standard Drupal user listing page ('admin/people'), you can replace it with a view. If you need to have the same kind of admin functions, consider using Administration Views which give you a number of default views you can modify to add in user pictures.

  • Thanks, but I'm trying to figure out why my call to theme doesn't return any output. I'm passing the correct arguments to the function, I think. There shouldn't be a permissions problem either because I'm user #1. – 24ma13wg Nov 26 '13 at 9:49
0

The code works fine, and when I directly copy-pasted it into my theme it worked. I only had to change the name of the function slightly to match my theme, of course.

So I can only speculate on why this will not work for you, and can only offer some suggestions on narrowing down the problem.

  1. You aren't naming your theme function correctly. Maybe you have a typo?
  2. Your theme isn't enabled.
  3. Your user configurations are incorrect.
  4. Your theme registry needs to be rebuilt.
  5. You put user-picture.tpl.php into your template directory but it is empty or the code in it doesn't do anything productive.

First, of course, clear your cache. Do this at admin/config/development/performance and click "Clear all caches." This will take care of #5.

Under /admin/config/people/accounts, verify you have user pictures enabled. Check "enable user pictures." Even go as far as specifying a default picture so at least something will get displayed at all times and your browser will show "broken image" placeholders. That will help with #4.

Verify /admin/appearance shows the correct configuration.

There are additional questions that need to be answered if problems persist. Is the code running at all?

Download the Devel module, enable it, and then add dsm("the theme is being called"); to the top of your theme function. When you view your user list you should see a message box at the top of the page spammed with a line per user.

If you get nothing, the function isn't even being called. If your function is getting called, move that dsm() statement around and try to find which conditional code is happening. You can also dsm($variables); to see if there's a problem there, as well.

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.