Hi,
I'm using the latest Private Messages 7.x and I'm trying to make my private messages threads and thread-list to look like Facebook... with user avatars. Kinda like this:
(I have done it in 5.x and 6.x), but in 7.x the function names (like privatemsg_list_field__participants) are no longer recognized. Below is the code that I put in my template.php for 6.x and it works like a charm (although slows down the pageload time). Would you please be so kind as to review the code below for 6.x and suggest what would be the optimal solution for 7.x
/**
* Theme the participants field.
*
* @see theme_privatemsg_list_field()
*/
function THEME-NAME_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;
// return theme('user_picture', $participant);
// select last author uid except the current user.
$uid = 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.
$account = user_load($uid);
if ($uid) {
return theme('user_picture', $account) . theme('username', $account);
}
}
/**
* Define the participants column.
*
* @see theme_privatemsg_list_header()
*/
function THEME-NAME_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
);
}
/* expriment to display From as avatars */
function THEME-NAME_privatemsg_username($user) {
return theme('user_picture', $user);
return theme('username', $user);
}
Please help, as I can see that many people are looking for a solution for this.
Many thanks in advance!
Comments
Comment #1
mototribe CreditAttribution: mototribe commentedgreat idea! I would love to see that too
Comment #2
drupalina CreditAttribution: drupalina commentedMy latest code looks like this:
but the problem is still in that line
which gives errors !!!
Can you help to make this work?
Everything works except for the line at
$uid = 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);
and gives me a serious error
Comment #3
drupalina CreditAttribution: drupalina commentedokey got it working with this code:
The pictures from the last participant who has replied are appearing.
Although if we have just sent a new message in a new thread, the "To" recipient's picture and username is not appearing... but I'll have to work on that.
Comment #4
drupalina CreditAttribution: drupalina commentedActually me rejoy was premature...
If Mark (sender) sends a message to Tom (recepient) and Tom doesn't reply, then Mark will see no user_picture or username of the Recepient (Tom). In such case, Mark should see Recepient's (Tom's) user_picture and username even if Tom has not replied yet.
Othervise it gives out a very ugly error message:
Could the above logic be incorporated into this function when determining the uid ?
If the resulting uid is that of Current user, then display the uid of the Recepient.
Comment #5
tanmaykI was also looking for the same but for D7. Here is how I got privatemsg list themed.
It will display the sender picture before the subject. Now we just need to do some CSS.
That is how I did it. Hope that will help someone for D7.
Regards,
Tanmay
Comment #6
Lordkien CreditAttribution: Lordkien commentedHi,
My need is to add new columns, modify/remove existing ones and rearrange the columns.
I've read the document but really don't understand much about this module API. The comments in the module told me to hook to privatemsg_list form and add new function to list_header and list_field. But still, I couldn't make it. Can anyone provide me with examples of how to do the aboves (beside modifying existing columns)?
Comment #7
ptmkenny CreditAttribution: ptmkenny commented@Lordkien This thread is about adding user pictures to private message list displays. If you want to change columns, you should open a different issue.
Comment #8
superdorx CreditAttribution: superdorx commentedThanks!
Comment #9
darksleuth CreditAttribution: darksleuth commentedThank you - this is great.
Currently going through Drupal 7 tutorials and starting a Zen subtheme - this is the first piece of code I have put into a template.php file - and it works.
Cheers.
Comment #10
Bill Choy CreditAttribution: Bill Choy commentedThanks ... worked great, except your missing the class on the
<td class="privatemsg-list-participants">
Comment #11
ptmkenny CreditAttribution: ptmkenny commentedMarked http://drupal.org/node/1392448 as a duplicate
Comment #12
Cromian CreditAttribution: Cromian commentedYour image showed a delete link in the inbox view. How did you build it?
Comment #13
ptmkenny CreditAttribution: ptmkenny commented@Cromian: There is a delete link shown by default in the inbox if the user has permission to delete private messages. If you have further questions, you should open a new issue, since a delete link really has nothing to do with the topic of this issue (displaying user pictures).
Comment #14
ptmkenny CreditAttribution: ptmkenny commentedComment #15
momo77 CreditAttribution: momo77 commentedRather than the user picture I'd like a picture field from the user profile to be displayed in the list of threads. How would I do that?
In this line:
$field['data'] = theme('user_picture', array('account' => $message->author));
I tried to replace 'user_picture' by my fields machine readable name 'field_image' but that didn't work.
Comment #16
ptmkenny CreditAttribution: ptmkenny commented@momo77 In that case, you need to pull in the image via Field API. Something like:
$account->field_my_field['und'][0]['value']
But this depends on whether your field allows multiple values (and how you want to handle the case of multiple values if it does). You'll also need to set which image style you want to use to display the image.
One alternative that could be easier is to use your field as the user picture:
http://drupal.stackexchange.com/questions/27005/how-can-i-display-a-pict...
Comment #18
prezaeis CreditAttribution: prezaeis commentedHi guys, sorry to open this again.
I been reading through this but i cant figure something out, i have disabled the User Avatar on my site and instead added an image field to the user profile (D7) which i want displayed in private messages, whats the best way to do this? i tried the method mentioned in #16 with no luck :(
Any help would be appreciated, im willing to pay
Thank you
Comment #19
ptmkenny CreditAttribution: ptmkenny commented@prezaeis You have to use the name of your field.
field_my_field
should be the machine name of your field.Comment #20
prezaeis CreditAttribution: prezaeis commentedI did change the name to the one i have setup,
Where does that line need to go? just to make sure im putting it in the correct place
Comment #21
RaulMuroc CreditAttribution: RaulMuroc commentedWhat about have also pictures in the thread of a messages, I mean:
INBOX => /messages
MESSAGE THREAD => /messages/view/message_id
Would be nice also in the 'conversation' itself.
Comment #22
RaulMuroc CreditAttribution: RaulMuroc commentedSolution for '/messages', not for '/messages/view/message_id'
Profile2 fields integration:
In your template.php override same function as follows:
Comment #23
jeffmatt CreditAttribution: jeffmatt commentedHello guys! I need help.
My motherlanguage is german and i have some problems to understand this post.
I´ve tried to put a user pic onto the messages/list and messages/sent page
I read every thread but still have no clue how i can make it work.
I tried to put the codes into the privatemsg.theme.inc : (???)
#5
#22
I'm not really used to PHP and normaly don't touch the template files.
so I'm a little confused about the lines;
function REPLACEYOURTHEMENAME
or
PHPTEMPLATE_privatemsg_list_field
Do I have to put the code in the privatemsg.theme.inc code or somewhere else?
Original code privatemsg.theme.inc :
Comment #24
RaulMuroc CreditAttribution: RaulMuroc commented@Jacky Pinkman, let's go step by step.
You can override functions, that means, you can write over/rewrite an existing functions inside modules with your custom code.
For that you can do module overriding (more for functionality) or template overriding (more for visual stuff).
Here we are doing template overriding.
So you do not have to change privatemsg.theme.inc directly because this file is inside the module "Privatemsg" and if in future you update this module to a newer version your changes will be lost and you will have to re-write again the code.
Best solution so is to write code in a place whatever you update (modules, themes, drupal core...) doesn't affect. Typically this is ../sites/all/themes/REPLACEYOURTHEMENAME/template.php for template overriding.
So, having in mind you have to modify template.php and not privatemsg.theme.inc, watching the whole code in #22 and suposing you are using GARLAND theme, inside template.php will be something similar to:
This last means, " ok, in the module privatemsg there is a function called phptemplate_privatemsg_list_field__subject($variables) and this is the function which paints my list of messages so if I wish modify this list to show attached user pictures then I have to modify this function through overriding it."
And that's what we did :) Hope it clarifies a little bit.
My recommendation, get some nice book of drupal to learn about, one that is cool: Drupal 7 themes
Cheers ;)
Comment #25
jweirather CreditAttribution: jweirather commentedA couple of thoughts/notes for future readers:
1) Many thanks to everybody on the thread, especially @drupalina #5 and @RaulMuroc #24
2) The code in #5 appears to use the avatar for the author who started the thread, versus the author who sent the most recent message in the thread. This appears to be by design, as showing the most recent author adds query overhead each time the list is loaded. The user who started the thread should be sufficient eye candy and functionality for most (of my) use cases.
3) In 7.x-1.4, the code in #5 worked like a charm for me, adding it to template.php as per #24, and is probably suitable for most users. For reasons I won't delve into, I decided to move the code to a custom "MYMODULE" module (with related settings page) instead of just using template.php, which is a little easier. In order to do to use a custom module instead of template.php, you need to ALSO add a "theme_registry_alter" function to your module file, to tell Drupal what function to use. This is true whenever you want to override _theme_ functions in your custom module instead of the template.php file, because Drupal does not by default look to module files for theme functions(?). I think.
Anyway, mine looked something like this:
(inside of a custom module called MYMODULE.module; replace "MYMODULE" with your module name)
For a short discussion about using template.php versus custom modules, see this thread:
http://drupal.stackexchange.com/questions/42212/custom-module-vs-templat...
Also read comment #24
Comment #26
System Lord CreditAttribution: System Lord commentedDont forget to clear cache!! Ughh what a nightmare. Got #5 working! Thank you for the code!
Now, however, what I need is only the intended recipient's and the sender's user picture to display. Never mine.
Use case: (piggy backing on #25 para 2)
Imagine a dating site's inbox/sent folder. You never see your own user picture whether you start the thread or not. When I send a new message to a recipient I need their user picture to be displayed in the /messages. Also, when I receive a new message from anyone I need their user picture displayed in the /messages/sent.
I also need "Subject" to be changed to "To" and "From" respectively. I don't have "Subject" available to users. Actually, I really don't need anything displayed. I could hit it with "visibility:hidden" But, this may be for another discussion than here.
Apologies...
Moved to: https://www.drupal.org/node/2402063
Comment #27
Triumphent CreditAttribution: Triumphent commentedHow do you add an image style to the code in #5?
Comment #28
tanmayk@Triumphent
You can change below line to get image using image style:
$field['data'] = theme('user_picture', array('account' => $message->author));
Instead of above line, get path to user picture image & use theme_image_style.
Comment #29
Triumphent CreditAttribution: Triumphent commented@tanmayk: Thank you.
Comment #30
hockey2112 CreditAttribution: hockey2112 commented@RaulMuroc, #21 worked for me. It displays the image in the Subject column, but I'd prefer to move it over to the Participants column where it displays the user's name. How would I do that?
Or... how can I add the user's name alongside the photo in the Subject line?
Comment #31
hockey2112 CreditAttribution: hockey2112 commentedHow would I grab another field from the user account and print it in the inbox? I created a custom image field (field_user_picture) on the account itself (/admin/config/people/accounts/fields), and I would now like to print that and use that as the inbox thumbnail.
Thanks!
Comment #32
hockey2112 CreditAttribution: hockey2112 commentedIn an effort to display the Profile2 image in the Participants column instead of the Subject column, I added the following to my template.php:
It is not adding the user image to the Participants column.
Can anyone tell me what I am doing wrong, and how I can use this code to add the user's image to the Participants column instead of the Subject column?
One other question... how can I modify the profile2 portion of the code so that the user's image links to their profile page (for example, mysite.com/members/username)
Comment #33
hockey2112 CreditAttribution: hockey2112 commentedOK so I tried the solution listed in #3, using the User's "picture" value. It works, but there is still the issue of no image showing up if the recipient has never replied, since the following code is only looking for UID for any user EXCEPT the current author:
Since there is only one UID, that of the original author, when the message has not yet been replied to, the username and photo do not display and that column is left blank.
How can this code be adjusted to fix this issue? Is there perhaps a way to show the thumbnail for all "other" users involved in the conversation? I suppose that could get unwieldy if you are in a huge group discussion, but I'd imaging those are rarer than one-to-one messaging.
The changes from #10 get me a little closer, in that the recipient user name is displayed in the un-replied-to message in the Sent folder, but it still is not displaying the recipient user's picture.
Comment #34
Anonymous (not verified) CreditAttribution: Anonymous commentedI just used this function in a custom service to return the user object with the data you are looking for i.e. the OTHER USER besides the author.
the variable $result is the user object so I think $result->picture->uri is the picture. You can also look up formatting the thumbnail using programmatic image styles.
Comment #35
Mihai-Alin CreditAttribution: Mihai-Alin at ASSIST Software SRL commentedHello! I would like to ask some help regarding the message body in the list.
Should I take the same approach as for the avatar, or how could i get something like "privatemsg_list_field__BODY" ?
Thank you!