Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
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;
}
Comments
Works with D7
I tried to make it with D7 and it's working fine.
I just had to add "recipient" in $blocked as followed
hook_privatemsg_block_message
The above hook implementation has a bug that took me a little while to work out. The $blocked array's first key is 'recipient', not 'uid' and the value for it is the key of the $recipients array, not the uid of the recipient. Here's the documentation for the hook: http://api.worldempire.ch/api/privatemsg/privatemsg.api.php/function/hoo...
and the version I have working:
Also, an implementation of hook_privatemsg_sql_autocomplete_alter that works with Friendlist: