Programmatically add new role to group member Trying to update a group membership to add an additional role, but cannot find any help on how to do this.

I want to add the role of "manager" to all owners of "client" type groups, using the following code:

// Get the group owner
$owner = $group-&gt;getOwner();

// Get the membership details of the owner
$membership = $group-&gt;getMember($owner);

// Get the roles of the owner
$roles = $membership-&gt;getRoles();

// Check whether the owner has manager role
if (!isset($roles['client-manager'])) {
  // No, so add the manager role to the membership
  // &gt;&gt;&gt; The following doesn't work, but I hope you get the idea &lt;&lt;&lt;
  $manager = new GroupRole();
  $membership-&gt;addRole($manager);
  $membership-&gt;save();
}


Any help or advice with this problem appreciated.

I was using hook_group_user_roles_alter(), but this has been removed in the latest release (8.x-1.0-beta5), so now need to implement a db update function to set it permanently.
I was eventually able to find the correct process.  The working code follows:

// Get the group owner
$owner = $group-&gt;getOwner();

// Get the membership details of the owner
$membership = $group-&gt;getMember($owner);

// Get the roles of the owner
$roles = $membership-&gt;getRoles();

// Check whether the owner has manager role
if (!isset($roles['client-manager'])) {
  // No, so add the manager role to the membership
  // Get the group_content entity
  $group_content = $membership-&gt;getGroupContent();
  // Set target group role
  $group_content-&gt;group_roles-&gt;target_id = 'client-manager';
  // Save updated entity
  $group_content-&gt;save();
}


Thanks for your help.