Question

In Drupal 7, I would like to set up a rule that sends an email to all users in an ORGANIC GROUPS Role, based on an action. I know how to get the action, I know how to do the loop, I know how to send the email.

I cannot, for the life of me, figure out how to get a list of group members with group role "X".

PS - I've reviewed this link: http://www.sthlmconnection.se/en/blog/rules-based-notifications-organic-groups , and it's for D6.

Was it helpful?

Solution 2

I've submitted a patch to a similar issue for OG https://drupal.org/node/1859698#comment-8719475 which should allow you to do this in Rules without the need for a custom module or needing to know the role id.

Once you apply the patch, you can use the "Get group members from group audience" action, and now filter by the "Membership States" and "Group Roles". Then add a loop to go through the list and use the "Send mail" action to send an email to each member.

OTHER TIPS

GAAH ARGH! (And much hair pulling later), here's the answer:

Custom Module (myutil.module) - .module file empty, .info file with the same sparse info required for any other module.

Add file myutil.rules.inc with following code:

/**
 * @file
 * Rules code: actions, conditions and events.
 */

/**
 * Implements hook_rules_action_info().
 */

function myutil_rules_action_info() {

  $actions = array(
    'myutil_action_send_email_to_group_editors' => array(
      'label'         => t('Get group editors from group audience'),
      'group'         => t('My Utilities'),
      'configurable'  => TRUE,
      'parameter' => array(
        'group_content' => array(
          'type' => 'entity',
          'label' => t('Group content'),
          'description' => t('The group content determining the group audience.'),
        ),
      ),
      'provides' => array(
        'group_editors' => array('type' => 'list<user>', 'label' => t('List of group editors')),
      ),
      'base'  => 'myutil_rules_get_editors',
    ),
  );

  return $actions;

}


function myutil_rules_get_editors($group_content) {

  if (!isset($group_content->og_membership)) {
    // Not a group content.
    return;
  }

  $members = array();
  foreach ($group_content->og_membership->value() as $og_membership) {
    // Get the group members the group content belongs to.
    $current_members = db_select('og_membership', 'om');
    $current_members->join('og_users_roles', 'ogur', 'om.etid = ogur.uid');
    $current_members->fields('om', array('etid'));
    $current_members->condition('om.gid', $og_membership->gid);
    $current_members->condition('om.entity_type', 'user');
    // FOR THIS LINE, YOU'LL NEED TO KNOW THE ROLE ID FROM THE `og_role` TABLE
    $current_members->condition('ogur.rid', 14);

    $result = $current_members->execute();
    while ($res = $result->fetchAssoc()) {
      $members[] = $res['etid'];
    }
  }
  // Remove duplicate items.
  $members = array_keys(array_flip($members));
  return array('group_editors' => $members);

}

Enable the module as you would any other module. Clear cache. Go back to Rules and enjoy.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top