You are here

public function SocialGroupHelperService::getAllGroupsForUser in Open Social 10.2.x

Same name and namespace in other branches
  1. 8.9 modules/social_features/social_group/src/SocialGroupHelperService.php \Drupal\social_group\SocialGroupHelperService::getAllGroupsForUser()
  2. 8.4 modules/social_features/social_group/src/SocialGroupHelperService.php \Drupal\social_group\SocialGroupHelperService::getAllGroupsForUser()
  3. 8.5 modules/social_features/social_group/src/SocialGroupHelperService.php \Drupal\social_group\SocialGroupHelperService::getAllGroupsForUser()
  4. 8.6 modules/social_features/social_group/src/SocialGroupHelperService.php \Drupal\social_group\SocialGroupHelperService::getAllGroupsForUser()
  5. 8.7 modules/social_features/social_group/src/SocialGroupHelperService.php \Drupal\social_group\SocialGroupHelperService::getAllGroupsForUser()
  6. 8.8 modules/social_features/social_group/src/SocialGroupHelperService.php \Drupal\social_group\SocialGroupHelperService::getAllGroupsForUser()
  7. 10.3.x modules/social_features/social_group/src/SocialGroupHelperService.php \Drupal\social_group\SocialGroupHelperService::getAllGroupsForUser()
  8. 10.0.x modules/social_features/social_group/src/SocialGroupHelperService.php \Drupal\social_group\SocialGroupHelperService::getAllGroupsForUser()
  9. 10.1.x modules/social_features/social_group/src/SocialGroupHelperService.php \Drupal\social_group\SocialGroupHelperService::getAllGroupsForUser()

Get all group memberships for a certain user.

Parameters

int $uid: The UID for which we fetch the groups it is member of.

Return value

array List of group IDs the user is member of.

File

modules/social_features/social_group/src/SocialGroupHelperService.php, line 192

Class

SocialGroupHelperService
Class SocialGroupHelperService.

Namespace

Drupal\social_group

Code

public function getAllGroupsForUser($uid) {
  $groups =& drupal_static(__FUNCTION__);

  // Get the memberships for the user if they aren't known yet.
  if (!isset($groups[$uid])) {

    // We need to get all group memberships,
    // GroupContentType::loadByEntityTypeId('user'); will also return
    // requests and invites for a given user entity.
    $group_content_types = GroupContentType::loadByContentPluginId('group_membership');
    $group_content_types = array_keys($group_content_types);
    $query = $this->database
      ->select('group_content_field_data', 'gcfd');
    $query
      ->addField('gcfd', 'gid');
    $query
      ->condition('gcfd.entity_id', $uid);
    $query
      ->condition('gcfd.type', $group_content_types, 'IN');
    $query
      ->execute()
      ->fetchAll();
    $group_ids = $query
      ->execute()
      ->fetchAllAssoc('gid');
    $groups[$uid] = array_keys($group_ids);
  }
  return $groups[$uid];
}