Using the privatemessage module, users can send messages to each other as in facebook. The page displays a form with a TO field, subject and message. The TO field is an autocomplete field by default. Upon searching for a user name, it shows a drop down with only the names. How can i customize it to show the user's profile picture also in the drop down??? I dont know where to go and make the code changes.

  • Have you tried posting a support request on the Privatemsg issue queue? The maintainer generally responds if you show that you made some effort to find a solution yourself. Commented May 4, 2012 at 1:59

2 Answers 2


Open privatemsg.module

in hook_menu search for $items['messages/user-name-autocomplete'] this particular menu item defines that auto complete path.

Search for privatemsg_user_name_autocomplete function where the output defines.

Edit this function. Thats all. Also Create a patch if youre familiar with patching, so that others c

  • I'm not a drupal expert. I dont know how to edit the code. I saw the code. It just returns a list of names i think. I have my own function to retrieve the user's profile picture. I just want to edit the code so that it returns an html output containing a div with profile pic and the name by side of image. The code is very confusing.
    – Rajesh
    Commented May 3, 2012 at 4:32

Note: Tested in Drupal 7 only, years later...

Here's an example of a modification that can be made to privatemsg_autocomplete in the privatemsg.pages.inc file such that a custom Profile2 picture field can be added instead of simply names.

I don't know how it performs though or if there are any scenarios under which it may fail. Compare the code with the original.

 * Return autocomplete results for usernames.
 * Prevents usernames from being used and/or suggested twice.
function privatemsg_autocomplete($string) {
  $names = array();
  // 1: Parse $string and build list of valid user names.
  $fragments = explode(',', $string);
  foreach ($fragments as $name) {
    if ($name = trim($name)) {
      $names[$name] = $name;
  // 2: Find the next user name suggestion.
  $fragment = array_pop($names);
  $matches = array();
  if (!empty($fragment)) {
    $remaining = 10;
    $types = privatemsg_recipient_get_types();
    foreach ($types as $name => $type) {
      if (isset($type['autocomplete']) && is_callable($type['autocomplete']) && privatemsg_recipient_access($name, 'write')) {
        $function = $type['autocomplete'];
        $return = $function($fragment, $names, $remaining);
        if (is_array($return) && !empty($return)) {
          $matches = array_merge($matches, $return);
        $remaining = 10 - count($matches);
        if ($remaining <= 0) {
  // Allow modules to alter the autocomplete list.
  drupal_alter('privatemsg_autocomplete', $matches, $names, $fragment);

  // Format the suggestions.
  $themed_matches = array();
  foreach ($matches as $key => $match) {
    $themed_matches[$key] = privatemsg_recipient_format($match, array('plain' => TRUE));
   $profile = '';
   if (isset($match->uid)) {
        $profile = profile2_load_by_user($match->uid);

    $picture = '';
    if(isset($profile['main']->field_profile_photo['und'][0]['uri'])) { 
        $picture = theme('image_style', array( 'path' =>  $profile['main']->field_profile_photo['und'][0]['uri'], 'style_name' => 'profile_thumbnail'));    
    } else {
        //I'm using sites/default/files/default_images/profile-default.jpg as the default image
        $picture = theme('image_style', array( 'path' =>  "public://default_images/profile-default.jpg", 'style_name' => 'profile_thumbnail'));
    $picture_matches[$key] = $picture;

  // Check if there are any duplicates.
  if (count(array_unique($themed_matches)) != count($themed_matches)) {
    // Loop over matches, look for duplicates of each one.
    foreach ($themed_matches as $themed_match) {
      $duplicate_keys = array_keys($themed_matches, $themed_match);
      if (count($duplicate_keys) > 1) {
        // There are duplicates, make them unique.
        foreach ($duplicate_keys as $duplicate_key) {
          // Reformat them with unique argument.
          $themed_matches[$duplicate_key] = privatemsg_recipient_format($matches[$duplicate_key], array('plain' => TRUE, 'unique' => TRUE));

  // Prefix the matches and convert them to the correct form for the
  // autocomplete.
  $prefix = count($names) ? implode(", ", $names) . ", " : '';
  $suggestions = array();
  foreach ($themed_matches as $key => $match) {
    $suggestions[$prefix . $match . ', '] = $picture_matches[$key] . $match;

  // convert to object to prevent drupal bug, see http://drupal.org/node/175361

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.