HowTo: Integrate Privatemsg with a friendlist module

Last updated on
30 April 2025

This page provides a skeleton to integrate a friendlist module with Privatemsg and provide features like autocomplete for friends and blocking messages from users that aren't friends.


function mymodule_privatemsg_sql_autocomplete(&$fragments, $search, $names) {
  global $user;
  // Extend the query that searches for usernames
  
  // $fragments is explained in the api documentation in detail
  
  // The query is already set up, it's searching for usernames which start with $search and are not $names (may be empty)
  // the main table is {user} a
  
  // for example, add a join on a table where the user connections are stored
  // and specify that only users connected with the current user should be loaded
  $fragments['inner_join'] = 'INNER JOIN {my_table} m ON (m.user1 = u.uid AND m.user2 = %d)';
  $fragments['query_args'][] = $user->uid;
}

// This blocks messages between users, who are not related, it uses universal_relation_api so in theory should work with User Relationships and FriendsList modules, tested with User Relationships.

function mymodule_privatemsg_block_message($author, $recipients) {
  $blocked = array();
  foreach ($recipients as $recipient) {
	if (!module_invoke_all('socnet_is_related', $author->uid, $recipient->uid)) {
	  $blocked[] = array(
        'uid' => $recipient->uid,
        'message' => t('!name is not a friend of yours.', array('!name' => $recipient->name))
      );
    }
  }
  return $blocked;
}

Help improve this page

Page status: Not set

You can: