You are here

function social_group_group_insert in Open Social 8.9

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

Implements hook_entity_insert().

On a new group insert, from the type open_group or closed_group the Owner gets the group manager role by default.

File

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

Code

function social_group_group_insert(GroupInterface $group) {

  // @Todo remove this when https://www.drupal.org/node/2702743 lands and make
  // sure the settings will be implemented accordingly.
  if ($group
    ->getGroupType()
    ->id() === 'open_group' || $group
    ->getGroupType()
    ->id() === 'closed_group' || $group
    ->getGroupType()
    ->id() === 'flexible_group') {

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

    // Get membership.
    $content = $group
      ->getMember($account)
      ->getGroupContent();

    // Delete the initial created membership.
    $content
      ->delete();
    $grant_group_admin = FALSE;

    // If the user has this permission inside a group.
    if ($group
      ->hasPermission('manage all groups', $account)) {

      // Then we grant this user de Group Admin role.
      $grant_group_admin = TRUE;
    }

    // When a CM+ creates a group, it is given the group_manager role
    // alongside the group_admin role to keep the full control over the group.
    if ($grant_group_admin) {

      // Delete the initial created membership.
      $content
        ->delete();
      $plugin = $group
        ->getGroupType()
        ->getContentPlugin('group_membership');
      $values = [
        'group_roles' => [
          $group
            ->bundle() . '-group_admin',
          $group
            ->bundle() . '-group_manager',
        ],
      ];
      $group_content = GroupContent::create([
        'type' => $plugin
          ->getContentTypeConfigId(),
        'gid' => $group
          ->id(),
        'entity_id' => $group
          ->getOwnerId(),
      ] + $values);
      $group_content
        ->save();
    }
    else {

      // Create a new membership.
      $plugin = $group
        ->getGroupType()
        ->getContentPlugin('group_membership');
      $values = [
        'group_roles' => [
          $group
            ->bundle() . '-group_manager',
        ],
      ];
      $group_content = GroupContent::create([
        'type' => $plugin
          ->getContentTypeConfigId(),
        'gid' => $group
          ->id(),
        'entity_id' => $group
          ->getOwnerId(),
      ] + $values);
      $group_content
        ->save();
    }
  }
}