In the to-field I can choose all website users. For a project I'm currently working on I need to send messages to (some) individual members of the group I'm subscribed to. Where do I start to try to develop this?

CommentFileSizeAuthor
#6 privatemsg_og.zip2.85 KBHansKuiters
Support from Acquia helps fund testing for Drupal Acquia logo

Comments

ptmkenny’s picture

Category: feature » support
jaxtheking’s picture

I had the same requirement. Actually, that's why I had never used the privatemsg module as I always needed to be able to exchange messages within group members only.
In our latest project, I really needed it, so I found possibly the easiest way of achieving this.

/**
 * Implements hook_query_privatemsg_autocomplete_alter().
 */
function mymodule_query_privatemsg_autocomplete_alter(SelectQueryInterface $query) {
  global $user;
  $account = user_load($user->uid);
  
  // Load all groups the user belongs to
  $gids = og_get_groups_by_user($account);
  $groups = array_keys($gids['node']);
  
  // Get all users within the user's groups
  $peers = db_select('og_membership', 'ogm');
  $peers->fields('ogm', array('etid'));
  $peers->condition('ogm.entity_type', 'user')
    ->condition('ogm.gid', $groups, 'IN')
    ->distinct();

  // Only include these as the possible recipients
  $query->condition('u.uid', $peers, 'IN');
}

Place this function in your own custom module (and replace 'mymodule' with your module name) and clear cache/visit the modules page.
Note: it currently works only with standard the group type (node).

HansKuiters’s picture

Issue summary: View changes

Thanks for this module, it works like a charm.

HansKuiters’s picture

I tweaked it a bit. It now checks for actually being group members available before altering the query. Otherwise the autocomplete throws an error.

/**
 * Implements hook_query_privatemsg_autocomplete_alter().
 */
function privatemsg_og_query_privatemsg_autocomplete_alter(SelectQueryInterface $query) {
  global $user;
  $account = user_load($user->uid);
  
  // Load all groups the user belongs to
  $gids = og_get_groups_by_user($account);
  $groups = array_keys($gids['node']);
  
  $peers = array(0);

  if ($groups != NULL) {
    // Get all users within the user's groups
    $peers = db_select('og_membership', 'ogm');
    $peers->fields('ogm', array('etid'));
    $peers->condition('ogm.entity_type', 'user')
      ->condition('ogm.gid', $groups, 'IN')
      ->distinct();
  }

  // Only include these as the possible recipients
  $query->condition('u.uid', $peers, 'IN');
}
Anonymous’s picture

Jax and Capono, thank you. This works great. Would be nice if we could get it committed as part of private messages. I guess it would have to be a module option; if the user is using OG, to permit them to turn this on/off. I don't know how to do that but it sure would be cool!

It took me a lot of searching to find this so my following words are added simply to help some other developer to find it:

This custom hook will narrow the list of names in private message to only those within the groups (Organic groups - OG) you are part of. So if you use /message/new and enter a persons name all the site names are searchable. With this hook only those names contained in the organic groups you are part of are found, thus honoring the Organic groups structure.

Thanks again!

HansKuiters’s picture

FileSize
2.85 KB

Works well with Privatemsg 7.x-2.x-dev. I attached a module for it. I don't have a D.O. dev-account so can't commit it.

HansKuiters’s picture

Status: Active » Needs review
CountPacMan’s picture

Status: Needs review » Reviewed & tested by the community

Tested - works great. Thanks jaxtheking and capono!

surendramohan’s picture

Thanks @HansKuiters for the module :) It works great! Except that it also includes the sender username in the list of recipients. It would be appreciated if it is fixed! Thanks again!

pinueve’s picture

+1 #6 your module just works great, thanks @HansKuiters for shearing it, +1#9, small fix that i think should be done.

SocialNicheGuru’s picture

Should this be an add on to private message or should it get it's own home?

ivnish’s picture

Status: Reviewed & tested by the community » Closed (outdated)