I would like to make a block for printing messages from the user.

I already have a snippet of code which print me the last 5 messages, but I want to add more information like the author and the date.

Snippet of code

global $user;

$query = _privatemsg_assemble_query('list', $user, 'inbox');

$list = array();
foreach ($query->execute() as $thread) {
  if ($count <=5) {
    $list[] = l($thread->subject, 'messages/view/'. $thread->thread_id);
  } else { 
 if ($list) { 
  print theme('item_list', array('items'=>$list));
   else {
echo '<p>You still have no message yet</p>';
  • So you have already retrieved the messages (referred to as $thread in the foreach loop) and you are able to build a list from that. That seems like a good start. What did you try so far to get the other data? What else is in the $thread object besides a subject and thread_id? Do you know how to use a debugger, set a breakpoint, and inspect variables? Feb 24, 2017 at 21:43

2 Answers 2


Using print_r($thread); in your code gives this.

stdClass Object
    [thread_id] => 2
    [subject] => Testing
    [last_updated] => 1443004733
    [has_tokens] => 0
    [is_new] => 0
    [last_reply_to_mid] => 2
    [participants] => user_187,user_298

The below gives an array of the participants names.

    $participants = $thread->participants;
    $msg_users = explode(",",$participants);
    $prefix = 'user_';

    $authors = array();
    foreach ($msg_users as $msg_user) {
        if (substr($msg_user, 0, strlen($prefix)) == $prefix) {
        $msg_user = substr($msg_user, strlen($prefix));
        $msg_user_names = user_load($msg_user)->name;
        $authors[] = $msg_user_names;

And something like the below gives the date.

print date("Y-m-d\TH:i:s\Z", $thread->last_updated);

I'm sure you can work out what to based on that.

ps. I've wanted this exact thing so thanks for the snippet :)

  • Hi, I'm so noob in php I've installed the module Privatemsg block which do the job. I hid every content I don't need with "display:none" classes. Not proper ... but fit my needs. Thanks you for your answer.
    – Aporie
    Feb 27, 2017 at 11:48

Niall Murphy pretty much summed up everything the OP wanted, but I was looking for the ability to also get the body of the message. So for those who are looking for a solution to get the body (or something else related to the thread), here's what I found.

This answer proved to be a great start: https://drupal.stackexchange.com/a/220672/61171

As described there, you can use the privatemsg_thread_load() function to load the entire thread/message array and get pretty much everything you need.

So I used the code in the OP's example to get the thread id and in the loop I loaded the thread with this:

$threadArray = privatemsg_thread_load($thread->thread_id, $account = NULL, $start = NULL, $useAccessDenied = FALSE);

You can tidy that up to fit your needs. But anyway, now if you do:


you will see the whole thread array and pick up what you need. For example to get the body, just go:

$body = $threadArray['messages'][$thread->thread_id]->body;

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.