Which hook to use to detect new members added to group? 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-&gt;id();
  $group_memberships = \Drupal::service('group.membership_loader')-&gt;loadByUser($user);
  foreach ($group_memberships as $group_membership) {
    $group = $group_membership-&gt;getGroup();
    $groupid = $group-&gt;id-&gt;value;
    $roles = $group_membership-&gt;getRoles();
    // Get roles.
    foreach ($roles as $rolename =&gt; $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?
this works:
function tm_mymodule_group_content_presave(Drupal\group\Entity\GroupContent $entity) {
  $group_content_type = $entity-&gt;getGroupContentType();
  if ($group_content_type-&gt;get('content_plugin') == 'group_membership') {    
    $groupid = $entity-&gt;gid-&gt;getValue()[0]['target_id'];
    $userid = $entity-&gt;entity_id-&gt;getValue()[0]['target_id'];
    $group = \Drupal::entityTypeManager()-&gt;getStorage('group')-&gt;load($groupid);
    $user = \Drupal::entityTypeManager()-&gt;getStorage('user')-&gt;load($userid);
    $membership = $group-&gt;getMember($user);
    $roles = $membership-&gt;getRoles();

     // Do something
   }
}