Problem/Motivation

At the moment there are not APIs to add or remove a role from a member.

I can add a role doing: $group_relationship->set('group_roles', [role_ids]);
Before that, I need to loop through the member roles and add all the existing role ids in an array and then add the new role.
Then save the Group relationships

$memeber = $group->getMember($account);
$existing_roles = $member->getRoles();
$g_relationship = $member->getGroupRelationship();
if (!isset($rexisting_roles[$new_role_id)) {
  $updated_roles = [];
  $updated_roles = $new_role;
  foreach($existing_roles as $existing_role) {
    if ($role->getScope() == 'individual') {
      $updated_roles[] = $existing_role->getOriginalId();
    }
  }
  $g_relationship->set('group_roles', $updated_roles);
  $g_relationship->save();
}

Is there a better way of doing it?

Comments

Vincenzo Gambino created an issue. See original summary.

vincenzo gambino’s picture

Issue summary: View changes
vincenzo gambino’s picture

Issue summary: View changes
kristiaanvandeneynde’s picture

Issue tags: +Group 3.3.0

Currently no, this has been on my list for a while so will look into addRole and removeRole.

kristiaanvandeneynde’s picture

Status: Active » Closed (duplicate)
Issue tags: -Group 3.3.0