Hi,

I'm migrating members from organic groups to Groups (D7 to D8). I tried to migrate the members to Groups via migration.yml file but it is not migrating. So i'm writing a query to fetch members from D7 and add it to D7 respective groups. Do you have any services to add a member to a group programmatically?

Comments

silambarasan created an issue. See original summary.

dsim’s picture

Issue summary: View changes
sumanthkumarc’s picture

AFAIK, there is a addMember method in group.php.

 public function addMember(UserInterface $account, $values = []) {
    if (!$this->getMember($account)) {
      $this->addContent($account, 'group_membership', $values);
    }
  }

You can load a group and then call addMember function on it.

dsim’s picture

Status: Active » Closed (works as designed)

Thanks sumanthkumarc for your input on this. It was helpful. Closing this ticket.

esrathi’s picture

I am loading the group and then calling addMember function on it, something like this:

$roles = array('test_role');
$values = ['group_roles' => $roles, 'gid' => 1];
$account = $this->createUser();
$group = \Drupal\group\Entity\Group::load(1);
$group->addMember($account, $values);

This is not adding the member to the group. Are $values and $account parameters correct here? Or is something else wrong?

apaderno’s picture

Priority: Major » Normal
Status: Closed (works as designed) » Fixed
Issue tags: -programmatically, -membership
apaderno’s picture

Status: Fixed » Closed (fixed)