Quantcast
Channel: Active questions tagged users - Drupal Answers
Viewing all articles
Browse latest Browse all 716

Which hook to use to detect new members added to group?

$
0
0

What is the best way to make a hook that only fires when new members are added to a group?

I have a function that exports group memberships to moodle course enrolments. Right now I have it bound to _user_presave:

function mymodule_user_presave (User $user) {  // Gather data.  $userid = $user->id();  $group_memberships = \Drupal::service('group.membership_loader')->loadByUser($user);  foreach ($group_memberships as $group_membership) {    $group = $group_membership->getGroup();    $groupid = $group->id->value;    $roles = $group_membership->getRoles();    // Get roles.    foreach ($roles as $rolename => $value) {

I bound it to user_presave after I realized that adding members to a group also calls this hook. But this seems unnecessarily inefficient, since it always returns all group memberships of the user, instead of just the latest one.

Is there a way to detect a new GroupMembership directly and hook into that?


Viewing all articles
Browse latest Browse all 716

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>