1

I am using the Drupal 7 version of the Group module. I found this Drupal 8 code which adds a user to a group, and I can't find anything similar for Drupal 7.

use Drupal\Core\Entity\ContentEntityBase;
$account = \Drupal\user\Entity\User::load('uid');
$group = \Drupal\group\Entity\Group::load($group_id);
$group->addMember($account);
$group->save();

What is the equivalent Drupal 7 code?

I tried using the following code, but it throws an error.

<?php
$account = user_load($uid);
$group = entity_load_single('group', 1);
$group->addMember($account);
$group->save();
?>

The error is the following one.

Object of class stdClass could not be converted to int in Group->addMember()

2
  • The ode you mentioned will not help with Drupal 7 for sure. I found using Rules modules with Organic Groups pretty easy and can cover almost all use-cases. Did you try it? Apr 10, 2020 at 7:41
  • Hi Hobda, thank you for replying. Yes I tried Rules before, I found it was helpful in most cases but I am trying to avoid using Rules if possible as for a few php executions it takes up a lot of resources. Apr 10, 2020 at 10:55

1 Answer 1

1

For D7 its

$account = user_load($uid);
$group = group_load($group_id);
$group->addMember($account->uid, array('joined_on' => REQUEST_TIME));

To delete

$group->removeMember($account->uid);

To request

$group->addMember($account->uid, array('status' => 'requested', 'requested_on' => REQUEST_TIME));

To block:

foreach (group_membership_load_by_user($account->uid) as $group_membership) {
  $block_gid = // some code to get the Group ID that you want the ...
               // ... the block to take place in.
  $gid = $group_membership->gid;
  if ($gid == $block_gid) {
    global $user;
    if ($group_membership->status != 'blocked') {
      $group_membership->blocked_status = $group_membership->status;
      $group_membership->status = 'blocked';
      $group_membership->blocked_on = REQUEST_TIME;
      $group_membership->blocked_by = $user->uid;
      $group_membership->save();
    }
}

To Unblock:

foreach (group_membership_load_by_user($account->uid) as $group_membership) {
    $unblock_gid = // some code to get the Group ID that you want the ...
                  // ... the unblock to take place in.
    $gid = $group_membership->gid;
    if ($gid == $unblock_gid) {
      if ($group_membership->status == 'blocked') {
        $group_membership->status = $group_membership->blocked_status;
        $group_membership->blocked_status = '';
        $group_membership->blocked_on = NULL;
        $group_membership->blocked_by = NULL;
        $group_membership->save();
      }
    }
}
2
  • Thank you so much @No Sssweat, such a simple bit of code but takes so long to find the correct format. I used the same to remove a member using removeMember. But could not find how to block a member, or request membership. Do you know these functions? Apr 10, 2020 at 11:25
  • @ImogenJohnson I have updated my answer to include those.
    – No Sssweat
    Apr 10, 2020 at 13:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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