2

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->getOwner();

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

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

// Check whether the owner has manager role
if (!isset($roles['client-manager'])) {
  // No, so add the manager role to the membership
  // >>> The following doesn't work, but I hope you get the idea <<<
  $manager = new GroupRole();
  $membership->addRole($manager);
  $membership->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 don't know group module, I'ts not clear to me what the entity types of $owner and $membership is. But if $owner is a user, then the addRole() argument excepts a user role string, not an object. – Berdir Mar 28 '17 at 20:52
  • $owner is an object of class AccountInterface (ie a user). $membership is an object of class Drupal\group\GroupMembership. $roles is an array of objects of class Drupal\group\Entity\GroupRole. Note that there is no "addRole" method available in GroupMembership (I wish there was). Therefore I am looking for something else that does an equivalent job. – jlscott Mar 28 '17 at 22:09
3

I was eventually able to find the correct process. The working code follows:

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

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

// Get the roles of the owner
$roles = $membership->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->getGroupContent();
  // Set target group role
  $group_content->group_roles->target_id = 'client-manager';
  // Save updated entity
  $group_content->save();
}

Thanks for your help.

  • 1
    My edit was reverted, however I think it's important and should be noted here. Use $group_roles->appendItem(['target_id' => $group_role]); in order to actually add role instead of overwriting all roles by new role. – Dmitriy Jun 8 '17 at 5:30
  • Good point @Dmitriy. However in my case, if a user has the "client-manager' role than they don't need, and probably shouldn't have any other role. – jlscott Jun 9 '17 at 7:48
  • FYI, I am using the same approach, but setting/resetting the entire group_roles array with $group_content->group_roles = $myRoleArray since we need to update multiple roles at once – theuni Jan 17 at 11:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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