2

Using the Group module, what is the function to remove a user from a group using its user ID and the group ID?

3

The entity type interface (GroupInterface) has a removeMember method; all you need are the relevant entity objects:

if ($account = \Drupal\user\Entity\User::load($user_id)) {
  if ($group = \Drupal\group\Entity\Group::load($group_id)) {
    $group->removeMember($account);
  }
}
  • I knew it had to be that simple! I was looking for a removeMember() method then after your answer I decided to look at the version I was using.... Thanks! – Dave S. Dec 6 '17 at 1:50
  • Do we have something like og_expire for drupal 8? Basically how to remove the users from the group automatically after a certain number of days? – Prerit Mohan Mar 29 at 16:35
  • In case anyone else is wondering, there is also $group->addMemeber($account); – wranvaud Aug 9 at 19:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.