You are here

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

Same name and namespace in other branches
  1. 10.3.x modules/social_features/social_group/src/SocialGroupHelperService.php \Drupal\social_group\SocialGroupHelperService::countGroupMembershipsForUser()
  2. 10.1.x modules/social_features/social_group/src/SocialGroupHelperService.php \Drupal\social_group\SocialGroupHelperService::countGroupMembershipsForUser()

Count all group memberships for a certain user.

Parameters

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

Return value

int Count of groups a user is a member of.

File

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

Class

SocialGroupHelperService
Class SocialGroupHelperService.

Namespace

Drupal\social_group

Code

public function countGroupMembershipsForUser($uid) : int {
  $count =& drupal_static(__FUNCTION__);

  // Get the count of memberships for the user if they aren't known yet.
  if (!isset($count[$uid])) {
    $hidden_types = [];
    $this->moduleHandler
      ->alter('social_group_hide_types', $hidden_types);
    $group_content_types = GroupContentType::loadByEntityTypeId('user');
    $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');
    if (!empty($hidden_types)) {
      foreach ($hidden_types as $group_type) {
        $query
          ->condition('gcfd.type', '%' . $this->database
          ->escapeLike($group_type) . '%', 'NOT LIKE');
      }
    }

    // We need to add another like for the fact that we have more plugins
    // than memberships for a User, like request or invite which are not
    // group memberships yet.
    $query
      ->condition('gcfd.type', '%group_membership', 'LIKE');

    // Add a query tag for other modules to alter, this query.
    $query
      ->addTag('count_memberships_for_user');
    $query
      ->execute()
      ->fetchAll();
    $group_ids = $query
      ->countQuery()
      ->execute()
      ->fetchField();
    $count[$uid] = $group_ids;
  }
  return $count[$uid];
}