I want to replace the participants list in the default module to sender. So I searched for it and found did the following as suggested at http://drupal.org/node/702934-

I went to the privatemsg.api.php folder of the module and pasted the above code as-

function hook_privatemsg_sql_participants_alter(&$fragment, $thread_id) {
global $user;

// Join table that contains author information.
$fragments['inner_join'][] = 'INNER JOIN {pm_message} pm ON (pm.mid = pmi.mid)';

// Only include the author of the first message of that thread and
// the current user (needed for permission checks).
$fragments['where'][] = '(pmi.uid = pm.author AND pmi.mid = pmi.thread_id) OR pmi.uid = %d';
$fragments['query_args']['where'][] = $user->uid;
}

But after doing all this, I am still getting all the participants of the message in the mail. How do i overcome this?
Thanks in advance~~!!
shklnrj.

CommentFileSizeAuthor
#3 preplacer.zip809 bytesshklnrj

Comments

berdir’s picture

There is no privatemsg.api.php folder, you need to create or extend an existing module use "modulename" instead of "hook".

See:
http://api.drupal.org/api/group/hooks/6
http://drupal.org/node/231276
http://geeksandgod.com/tutorials/computers/cms/drupal/creating-simple-dr...

Setting this to fixed since these are generic drupal questions, check the forums or IRC if you need help with creating modules/hooks.

Also note that this will just change the view at messages/view/thread_id, not the message list, that can currently not be easily changed.

berdir’s picture

Status: Active » Fixed
shklnrj’s picture

Status: Fixed » Active
StatusFileSize
new809 bytes

I followed the methodology after reading the articles in the above links and made a module which is attached. I still can see all the participants in a message instead of the author.

Comments ??

berdir’s picture

The attached module does what it's supposed to do.

It does remove the other recipients when *viewing a thread* at messages/view/threadid. It does not change the list at messages. That is possible too, but needs a different hook.

berdir’s picture

Status: Active » Postponed (maintainer needs more info)
YK85’s picture

Hi shklnrj,

I was wondering if u had any further progress with the custom module?
If so, would you kindly share it with the community?

Thanks!

andrew.lansdowne’s picture

If you want to display sender only rather than all participants in the list view, simply paste this function into your theme's template.php (works with privatemsg version 6.x-1.1) - Don't forget to change the THEMENAME to your theme name:

/**
 * Theme the participants field.
 *
 * @see theme_privatemsg_list_field()
 */
function THEMENAME_privatemsg_list_field__participants($thread) {
  $participants = _privatemsg_generate_user_array($thread['participants'], -4);
  
  // Remove ourselves from the list of participants
  global $user;
  foreach($participants as $key => $participant) {
  	if ($participant->uid == $user->uid) {
  		unset($participants[$key]);
  	}
  }
  
  $field = array();
  $field['data'] = _privatemsg_format_participants($participants, 3, TRUE);
  $field['class'] = 'privatemsg-list-participants';
  return $field;
}

It just removes the current user from the list of participants. Then you can also rename the column with this one:

/**
 * Define the participants column.
 *
 * @see theme_privatemsg_list_header()
 */
function THEMENAME_privatemsg_list_header__participants() {
  return array(
    'data'    => t('From / To'),    //<!----- NAME IT HERE
    'key'     => 'participants',
    'class'   => 'privatemsg-header-participants',
    '#weight' => -50,        //<!------ MOVE COLUMN TO LEFT
  );
}

Hope that helps.

EDIT: Changed the column name from "From" to "From / To" because privatemsg shows outgoing messages in the same table as incoming, and it is confusing to see message you've just sent appearing as if they were sent From your buddy.

Andy

berdir’s picture

Edit: Misread the above, it just removes the current users, that should work correctly.

berdir’s picture

Category: task » support
Status: Postponed (maintainer needs more info) » Fixed

It seems that this has either been resolved in the mean time or there was no response in a long time, closing this issue.

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.

drupalina’s picture

the code in #7 worked for me. Thank you andrew.lansdowne!!!

Now, how can I replace the username of the participant to display an avatar with his/her username as an alt???

drupalina’s picture

I almost got it. Just need a little push.
I used return theme('user_picture', $participant); instead of return $field; in code given in #7 , but that returns the picture of the original author of the tread, rather than the picture of the last responder.
How can I make it to return the picture of the last responder?

berdir’s picture

That is not easily possible because the list is not really sorted ( and right now, unfortunately, can't be sorted).

Maybe something like the following could work, but this is untested.

function THEMENAME_privatemsg_list_field__participants($thread) {
  // select last author uid.
  $uid = db_result(db_query('SELECT pm.author FROM {pm_message} pm INNER JOIN {pm_index} pmi ON pmi.mid = pm.mid AND pmi.thread_id = %d ORDER BY pm.timestamp DESC LIMIT 1', $thread['thread_id']));
  return theme('user_picture', user_load($uid));
}

Note that this is just hacked together and might or might not work. Also, it will execute multiple additional queries for every displayed thread.

PS: Please open new issues or at least re-open the issue so that it is displayed in the list or I might not see your questions.

drupalina’s picture

Version: » 6.x-1.2
Status: Closed (fixed) » Active

I'm gonna reopen this issue because it relates to the original topic.

@ Berdir thanks a lot for your code! It works, but only half way. It displays the last responder's picture, but it does so even if the last responder is ME. If the last responder is me if ($uid == $user->uid), then to ME it should display the picture of the original author of the thread, the same logic as on Facebook's messages. So, I think we need another 1 line.

It would also be nice to see the usernames next to the avatars too. Basically I'm trying to replicate the exact Facebook message listings look and structure.

berdir’s picture

Status: Active » Postponed (maintainer needs more info)

I guessed as much, the problem here is the special case "message with only a single author - the current user". Because without that special case, what you are asking for is as simply as adding a condition like this:

function THEMENAME_privatemsg_list_field__participants($thread) {
  global $user;
  // select last author uid except the current user.
  $uid = db_result(db_query('SELECT pm.author FROM {pm_message} pm INNER JOIN {pm_index} pmi ON pmi.mid = pm.mid AND pmi.thread_id = %d WHERE pm.author <> %d ORDER BY pm.timestamp DESC LIMIT 1', $thread['thread_id'], $user->uid));
  // Only display something if we have an uid.
  if ($uid) {
    return theme('user_picture', user_load($uid));
  }
}

If there is now a thread with a single message sent by the current user, this will simply display nothing. If that works for you, then fine...

To display the username beneath the picture, you can extend the "return .... " line as following:

$account = user_load($uid));
return theme('user_picture', $account) . theme('username', $account);

This will show both the profile picture and the username, you will probably need some CSS to have them show up in the way you want.

Please set to active again if you have more questions or fixed if it works.

Also, you are welcome to create a new documentation page in the handbook (http://drupal.org/handbook/modules/privatemsg) explaining what you have done and how. Thanks!

berdir’s picture

Status: Postponed (maintainer needs more info) » Fixed

There was no feedback so I assume that it did work. Marking this issue as fixed.

YK85’s picture

Hi,

I'm using 6.x-2.x and have it so users send messages to only one recipient at a time.
I want to change Participants to show the other user (ie remove the current user).
Would the below code work for 6.x-2.x in template.php or should I be doing this in a custom module?

/**
* Theme the participants field.
*
* @see theme_privatemsg_list_field()
*/
function THEMENAME_privatemsg_list_field__participants($thread) {
  $participants = _privatemsg_generate_user_array($thread['participants'], -4);
 
  // Remove ourselves from the list of participants
  global $user;
  foreach($participants as $key => $participant) {
      if ($participant->uid == $user->uid) {
          unset($participants[$key]);
      }
  }
 
  $field = array();
  $field['data'] = _privatemsg_format_participants($participants, 3, TRUE);
  $field['class'] = 'privatemsg-list-participants';
  return $field;
}

berdir’s picture

That should work I think. If it doesn't, give me an actual error/problem :)

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.

mpompili’s picture

I've divided Inbox from sent messages with the configuration option on: /admin/settings/messages

Choose the default list option: Inbox (x)

I guess you have to have privatemsg filter installed.

Then i check the url with this php snippet:

function THEMENAME_privatemsg_list_header__participants() {

  if(strstr($_SERVER['REQUEST_URI'],'sent')) $col = 'Receiver';
  else $col = 'Sender';

  return array(
    'data'    => t($col), //<!----- NAME IT HERE
    'key'     => 'participants',
    'class'   => 'privatemsg-header-participants',
    '#weight' => -50, //<!------ MOVE COLUMN TO LEFT
  );
}

So i can put receiver or sender in the inbox page and in the sent messages page.

j0e’s picture

Marco thanks so much for #20, works really well at dividing things properly. Much appreciated.
Joseph

mpompili’s picture

Version: 6.x-1.2 » 6.x-1.4

Hi Joseph. Glad to know that my code helped you out. :)

I've found out that if there's a checkbox on the left (needed to select a thread), the weight should be set to -49, so the checkbox is still on the first column on the left. Also this is a slightly shorter version.

function uwc_05_2_0_privatemsg_list_header__participants()
{
	return array(
		'data'    => t((strstr($_SERVER['REQUEST_URI'],"sent")) ? "Receiver" : "Sender"),
		'key'     => 'participants',
		'class'   => 'privatemsg-header-participants',
		'#weight' => -49,
	);
}
mikestar5’s picture

#17

Very good solution!

Thanks