How do I add a user to a group? 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-&gt;addMember($account);
$group-&gt;save();


What is the equivalent Drupal 7 code?

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

&lt;?php
$account = user_load($uid);
$group = entity_load_single('group', 1);
$group-&gt;addMember($account);
$group-&gt;save();
?&gt;


The error is the following one. 


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

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


To delete

$group-&gt;removeMember($account-&gt;uid);


To request

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


To block:

foreach (group_membership_load_by_user($account-&gt;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-&gt;gid;
  if ($gid == $block_gid) {
    global $user;
    if ($group_membership-&gt;status != 'blocked') {
      $group_membership-&gt;blocked_status = $group_membership-&gt;status;
      $group_membership-&gt;status = 'blocked';
      $group_membership-&gt;blocked_on = REQUEST_TIME;
      $group_membership-&gt;blocked_by = $user-&gt;uid;
      $group_membership-&gt;save();
    }
}


To Unblock:

foreach (group_membership_load_by_user($account-&gt;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-&gt;gid;
    if ($gid == $unblock_gid) {
      if ($group_membership-&gt;status == 'blocked') {
        $group_membership-&gt;status = $group_membership-&gt;blocked_status;
        $group_membership-&gt;blocked_status = '';
        $group_membership-&gt;blocked_on = NULL;
        $group_membership-&gt;blocked_by = NULL;
        $group_membership-&gt;save();
      }
    }
}