You are here

function _social_group_grant_admin_role in Open Social 8.9

Same name and namespace in other branches
  1. 8 modules/social_features/social_group/social_group.module \_social_group_grant_admin_role()
  2. 8.2 modules/social_features/social_group/social_group.module \_social_group_grant_admin_role()
  3. 8.3 modules/social_features/social_group/social_group.module \_social_group_grant_admin_role()
  4. 8.4 modules/social_features/social_group/social_group.module \_social_group_grant_admin_role()
  5. 8.5 modules/social_features/social_group/social_group.module \_social_group_grant_admin_role()
  6. 8.6 modules/social_features/social_group/social_group.module \_social_group_grant_admin_role()
  7. 8.7 modules/social_features/social_group/social_group.module \_social_group_grant_admin_role()
  8. 8.8 modules/social_features/social_group/social_group.module \_social_group_grant_admin_role()
  9. 10.3.x modules/social_features/social_group/social_group.module \_social_group_grant_admin_role()
  10. 10.0.x modules/social_features/social_group/social_group.module \_social_group_grant_admin_role()
  11. 10.1.x modules/social_features/social_group/social_group.module \_social_group_grant_admin_role()
  12. 10.2.x modules/social_features/social_group/social_group.module \_social_group_grant_admin_role()

This function checks if the user should get the admin role within a group.

Also check if the user has already a membership. If so, update it with the new role.

2 calls to _social_group_grant_admin_role()
social_group_group_content_insert in modules/social_features/social_group/social_group.module
When creating a new group membership.
social_group_group_content_update in modules/social_features/social_group/social_group.module
When updating a group membership.

File

modules/social_features/social_group/social_group.module, line 928
The Social group module.

Code

function _social_group_grant_admin_role($uid, $gid) {
  $account = User::load($uid);
  $group = Group::load($gid);

  // Must be a valid account AND a valid group.
  if ($account instanceof User && $group instanceof Group) {

    // Must have manage all groups permission.
    // Otherwise normal flow will be fine.
    if (!$account
      ->hasPermission('manage all groups')) {
      return;
    }

    // Check if the user is already a member in the group
    // (could be in update mode here).

    /** @var \Drupal\group\GroupMembership $membership */
    $admin_role = $group
      ->bundle() . '-group_admin';
    $membership = $group
      ->getMember($account);

    // Check what roles are there.
    $roles = [];
    foreach ($membership
      ->getGroupContent()->group_roles as $group_role_ref) {
      $roles[] = $group_role_ref->target_id;
    }

    // No admin? Add it.
    if (!in_array($admin_role, $roles)) {
      $membership
        ->getGroupContent()->group_roles[] = [
        'target_id' => $admin_role,
      ];
      $membership
        ->getGroupContent()
        ->save();
    }
  }
}