Updated about 9 yrs, 2 mths ago (September 30, 2015). Know a better answer? Let me know!
Send an email to all group managers via a reactive rule in Drupal 7
The idea was that when someone signed up and requested to join a group, the administrators (not just the author) of that group would get an email letting them know someone had requested access.
I recently had to send an email to all the administrators of the current user’s Drupal organic group, via a Rules reaction rule. Because this was triggered by a pending account activation using a one-time login link, I wasn’t able to easily get the administrators of the current user’s group.
The idea was that when someone signed up and requested to join a group, the administrators (not just the author) of that group would get an email letting them know someone had requested access.
Anyway, in case it’s of use, here’s how I got all the administrators of an organic group, as a Rules list variable, and then used that to send an email to all the group administrators, once someone activated their account.
Rules configuration can be found at URL:
/admin/config/workflow/rules
Here we have a rule which reacts to the event “When the user account is activated”:
Inside that rule, we check a few things such as the user who has just activated their account actually has an existing group (in this case, they pick one on signup), then we get a list of the group managers, loop through it, and send them each an email:
- Get a list of group managers
This uses the custom code explained below - Loop through this list of group managers
- Send an email to each group manager
To create the action Get group managers for current user, you need to create a module (or tack this onto an existing one if you have something suitable you’ve already written), as below:
To create a new module:
- Create my_module_name.info
name = My Module Name description = "Whatever." package = "Whatever" dependencies[] = og core = 7.x version = 7.x-0.1 files[] = my_module_name.module
- Create my_module_name.module
This file can be left blank, as it is not used. - Create my_module_name.rules.inc
This file is loaded by the Rules module, and the code below creates your new action - Clear Drupal’s cache
<?php /** * Implement hook_rules_action_info(). */ function my_module_name_rules_action_info() { return array( 'my_module_name_get_managers' => array( 'label' => t('Get group managers for current user'), 'group' => t('Custom'), 'provides' => array( 'group_managers' => array('type' => 'list', 'label' => t('List of group managers')), ), 'base' => '_my_module_name_get_group_managers' // 'access callback' => 'access_callback_function', ), ); } function _my_module_name_get_group_managers() { global $user; $uid = $user->uid; if($uid === 0) { // not logged in watchdog('rules', 'No logged in user found', array(), WATCHDOG_WARNING); return; } $account = user_load($user->uid); if(!isset($account->og_service[LANGUAGE_NONE][0]['target_id'])) { // no service found watchdog('rules', 'No service found', array(), WATCHDOG_WARNING); return; } // get group id // this is specific to my implementation - the user has a field // called "og_group" which stores their group // your implementation would probably vary $gid = $account->og_group[LANGUAGE_NONE][0]['target_id']; $members = array(); $current_members = db_select('og_users_roles', 'ogur'); $current_members->fields('ogur', array('uid')); $current_members->condition('ogur.rid', 3, '>='); $current_members->condition('ogur.gid', $gid); $result = $current_members->execute(); while ($res = $result->fetchAssoc()) { $members[] = $res['uid']; } // Remove duplicate items. $members = array_keys(array_flip($members)); // get user objects $return = array(); foreach($members as $key => $val) { $return[] = user_load($val); } return array('group_managers' => $return); }
More Information
Information above gathered from the following sources:
Updated about 9 yrs, 2 mths ago (September 30, 2015). Know a better answer? Let me know!
Related categories
.User submitted comments:
Comment on this article (no HTML, max 1200 characters):
ainoidoi89, about 5 yrs, 5 mths ago
Thursday July 4, 2019 3:34 AM
Now, for using rules to send emails to a group users has same role, you can apply this patch:
https://www.drupal.org/project/rules/issues/2409059
#44 worked for me on Drupal 8.7.1