Hello guys,

Is there a way I can display only the Unread Messages inside a block/page?

Comments

berdir’s picture

Status: Active » Fixed

You can do that in two steps, but you need some php code/knowledge.

1. Either call drupal_get_form('privatemsg_list', 'unread') (which will return the whole page from /messages) or "global $user; $query = _privatemsg_assemble_query('list', $user, 'unread');" (which will just create the necessary query for you in $query['query'] that you then need to use and build your list/page yourself. See #492822: to display latest 5 (more or less) messages for more information about that. See also http://blog.worldempire.ch/api/function/privatemsg_filter_menu/1 for an example to create a menu entry for the first example.

2. Define what the unread query type means by creating the following hook:

function yourmodule_privatemsg_sql_list_alter(&$fragments, $account, $argument) {
  if ($argument == 'unread') {
    // Only show thread that have atleast one new message.
    $fragments['having'][] = 'SUM(pmi.is_new) > 1';
  }
}

If you have generic php questions that are not specific to privatemsg.module, please ask them in the forums or irc.

achilles085’s picture

Thank you Berdir!

I followed your suggestion to use drupal_get_form()
for it was easier for me to follow.

what i did is i created a module

first, create a .info file named it as privatemsg_unread.info

; $Id: privatemsg_unread.info $
name = Show Unread Private messages
description = Show Unread Private messages
package = CUSTOM MODULE
core = 6.x

Next, Create the module file,named it as privatemsg_unread.module

<?php
function privatemsg_unread_menu() {
  $items['messages/unread'] = array(
    'title'            => 'Unread',
    'page callback'    => 'show_unread_messages',
    'access callback'  => 'privatemsg_user_access',
    'type'             => MENU_LOCAL_TASK,
    'weight'           => -20,
  );
  return $items;
}
function show_unread_messages(){
  $unread_msgs = drupal_get_form('privatemsg_list', 'unread');
  return $unread_msgs;
}
function privatemsg_unread_privatemsg_sql_list_alter(&$fragments, $account, $argument) {
  if ($argument == 'unread') {
    /** Only show thread that have atleast one new message. */
    $fragments['having'][] = 'SUM(pmi.is_new) > 1';
  }
}
?>

Put it inside a folder privatemsg_unread upload to your modules folder(usually sites/all/modules)
enable the module(admin/build/module) then voila!!!

To test(if only you have recent unread messages, otherwise no messages will be displayed) ,
try to access www.YOURSITE.com/messages/unread

Feel free to add some more..

berdir’s picture

Great, a few comments about your code.

>
function privatemsg_unread_menu() {
  $items['messages/unread'] = array(
    'title'            => 'Unread',
    'page callback'    => 'show_unread_privatemsg',
    'access callback'  => 'privatemsg_user_access',
    'type'             => MENU_LOCAL_TASK,
    'weight'           => -20,
  );
  return $items;
}
function show_unread_privatemsg(){
  $unread_msgs = drupal_get_form('privatemsg_list', 'unread');
  return $unread_msgs;
}

You don't need the second function. Just use 'drupal_get_form' as 'page callback' and then array('privatemsg_list', 'unread') as 'page arguments'.

>
function dealbacker_custom_privatemsg_privatemsg_sql_list_alter(&$fragments, $account, $argument) {

You probably have another module called dealbacker_custom so that hook works but it won't if someone else doesn't have that. also, it is a bit confusing :)

achilles085’s picture

sorry for that...i already updated it.

Another question if you don't mind

<?php
function privatemsg_unread_menu() {
  $items['messages/unread'] = array(
    'title'            => 'Unread',
    'page callback'    => 'drupal_get_form',
    'page arguments' => array('privatemsg_list', 'unread'),
    'access callback'  => 'privatemsg_user_access',
    'type'             => MENU_LOCAL_TASK,
    'weight'           => -20,
  );
  return $items;
}
?>

If i use this kind of hook menu...what path/file should i call?

I really like the idea of using api's to alter sql queries same as using hook_views_query_alter

thanks again man! you save me a alot of work! :)

Status: Fixed » Closed (fixed)

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