You are here

public function SocialProfileTrait::getUserIdsFromName in Open Social 8.2

Same name and namespace in other branches
  1. 8.9 modules/social_features/social_profile/src/SocialProfileTrait.php \Drupal\social_profile\SocialProfileTrait::getUserIdsFromName()
  2. 8.3 modules/social_features/social_profile/src/SocialProfileTrait.php \Drupal\social_profile\SocialProfileTrait::getUserIdsFromName()
  3. 8.4 modules/social_features/social_profile/src/SocialProfileTrait.php \Drupal\social_profile\SocialProfileTrait::getUserIdsFromName()
  4. 8.5 modules/social_features/social_profile/src/SocialProfileTrait.php \Drupal\social_profile\SocialProfileTrait::getUserIdsFromName()
  5. 8.6 modules/social_features/social_profile/src/SocialProfileTrait.php \Drupal\social_profile\SocialProfileTrait::getUserIdsFromName()
  6. 8.7 modules/social_features/social_profile/src/SocialProfileTrait.php \Drupal\social_profile\SocialProfileTrait::getUserIdsFromName()
  7. 8.8 modules/social_features/social_profile/src/SocialProfileTrait.php \Drupal\social_profile\SocialProfileTrait::getUserIdsFromName()
  8. 10.3.x modules/social_features/social_profile/src/SocialProfileTrait.php \Drupal\social_profile\SocialProfileTrait::getUserIdsFromName()
  9. 10.0.x modules/social_features/social_profile/src/SocialProfileTrait.php \Drupal\social_profile\SocialProfileTrait::getUserIdsFromName()
  10. 10.1.x modules/social_features/social_profile/src/SocialProfileTrait.php \Drupal\social_profile\SocialProfileTrait::getUserIdsFromName()
  11. 10.2.x modules/social_features/social_profile/src/SocialProfileTrait.php \Drupal\social_profile\SocialProfileTrait::getUserIdsFromName()

Get a list of account IDs whose account names begin with the given string.

Parameters

string $name: The string to search for.

int $count: The number of results to return.

string $suggestion_format: (optional) The suggestion format.

Return value

int[] An array of account IDs for accounts whose account names begin with the given string.

2 calls to SocialProfileTrait::getUserIdsFromName()
AutocompleteController::suggestions in modules/social_features/social_mentions/src/Controller/AutocompleteController.php
Function for suggestions.
PrivateMessageMapper::getUserIdsFromString in modules/social_features/social_private_message/src/Mapper/PrivateMessageMapper.php

File

modules/social_features/social_profile/src/SocialProfileTrait.php, line 26

Class

SocialProfileTrait
Trait SocialProfileTrait.

Namespace

Drupal\social_profile

Code

public function getUserIdsFromName($name, $count, $suggestion_format = SOCIAL_PROFILE_SUGGESTIONS_ALL) {
  $connection = \Drupal::database();

  // Nickname.
  $addNickName = \Drupal::moduleHandler()
    ->moduleExists('social_profile_fields');
  $query = $connection
    ->select('users', 'u')
    ->fields('u', [
    'uid',
  ]);
  $query
    ->join('users_field_data', 'uf', 'uf.uid = u.uid');
  $query
    ->condition('uf.status', 1);
  $name = $connection
    ->escapeLike($name);
  switch ($suggestion_format) {
    case SOCIAL_PROFILE_SUGGESTIONS_USERNAME:
      $query
        ->condition('uf.name', '%' . $name . '%', 'LIKE');
      break;
    case SOCIAL_PROFILE_SUGGESTIONS_FULL_NAME:
      $query
        ->join('profile', 'p', 'p.uid = u.uid');
      $query
        ->join('profile__field_profile_first_name', 'fn', 'fn.entity_id = p.profile_id');
      $query
        ->join('profile__field_profile_last_name', 'ln', 'ln.entity_id = p.profile_id');

      // Add nickname.
      if ($addNickName === TRUE) {
        $query
          ->leftJoin('profile__field_profile_nick_name', 'nn', 'nn.entity_id = p.profile_id');
      }
      $or = $query
        ->orConditionGroup();
      $or
        ->condition('fn.field_profile_first_name_value', '%' . $name . '%', 'LIKE')
        ->condition('ln.field_profile_last_name_value', '%' . $name . '%', 'LIKE');

      // Add Nickname.
      if ($addNickName === TRUE) {
        $or
          ->condition('nn.field_profile_nick_name_value', '%' . $name . '%', 'LIKE');
      }
      $query
        ->condition($or);
      break;
    case SOCIAL_PROFILE_SUGGESTIONS_ALL:
      $query
        ->leftJoin('profile', 'p', 'p.uid = u.uid');
      $query
        ->leftJoin('profile__field_profile_first_name', 'fn', 'fn.entity_id = p.profile_id');
      $query
        ->leftJoin('profile__field_profile_last_name', 'ln', 'ln.entity_id = p.profile_id');

      // Add nickname.
      if ($addNickName === TRUE) {
        $query
          ->leftJoin('profile__field_profile_nick_name', 'nn', 'nn.entity_id = p.profile_id');
      }
      $or = $query
        ->orConditionGroup();
      $or
        ->condition('uf.name', '%' . $name . '%', 'LIKE')
        ->condition('fn.field_profile_first_name_value', '%' . $name . '%', 'LIKE')
        ->condition('ln.field_profile_last_name_value', '%' . $name . '%', 'LIKE');

      // Add Nickname.
      if ($addNickName === TRUE) {
        $or
          ->condition('nn.field_profile_nick_name_value', '%' . $name . '%', 'LIKE');
      }
      $query
        ->condition($or);
      break;
  }
  $result = $query
    ->range(0, $count)
    ->execute()
    ->fetchCol();
  return !empty($result) ? $result : [];
}