You are here

protected function UserSelection::buildEntityQuery in Open Social 8.2

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

Builds an EntityQuery to get referenceable entities.

Parameters

string|null $match: (Optional) Text to match the label against. Defaults to NULL.

string $match_operator: (Optional) The operation the matching should be done with. Defaults to "CONTAINS".

Return value

\Drupal\Core\Entity\Query\QueryInterface The EntityQuery object with the basic conditions and sorting applied to it.

Overrides UserSelection::buildEntityQuery

File

modules/social_features/social_profile/src/Plugin/EntityReferenceSelection/UserSelection.php, line 35

Class

UserSelection
Provides specific access control for the user entity type.

Namespace

Drupal\social_profile\Plugin\EntityReferenceSelection

Code

protected function buildEntityQuery($match = NULL, $match_operator = 'CONTAINS') {
  $query = $this->connection
    ->select('profile', 'p')
    ->fields('p', [
    'uid',
  ]);
  $addNickName = $this->moduleHandler
    ->moduleExists('social_profile_fields');

  // Give the query a tag to identify it for altering.
  $query
    ->addTag('social_entityreference');
  $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');
  $query
    ->join('users_field_data', 'ufd', 'ufd.uid = p.uid');
  if ($addNickName === TRUE) {
    $query
      ->leftJoin('profile__field_profile_nick_name', 'nn', 'nn.entity_id = p.profile_id');
  }
  $name = $this->connection
    ->escapeLike($match);
  $or = $query
    ->orConditionGroup();
  $or
    ->condition('ufd.name', '%' . $name . '%', 'LIKE');
  $or
    ->condition('fn.field_profile_first_name_value', '%' . $name . '%', 'LIKE');
  $or
    ->condition('ln.field_profile_last_name_value', '%' . $name . '%', 'LIKE');
  if ($addNickName === TRUE) {
    $or
      ->condition('nn.field_profile_nick_name_value', '%' . $name . '%', 'LIKE');
  }

  // Only allow searching when user has permission to view.
  if ($this->currentUser
    ->hasPermission('view profile email')) {
    $or
      ->condition('ufd.mail', '%' . $name . '%', 'LIKE');
  }
  $ids = $query
    ->condition($or)
    ->execute()
    ->fetchCol();
  if (empty($ids)) {
    return parent::buildEntityQuery($match, $match_operator);
  }
  $query = parent::buildEntityQuery(NULL, $match_operator);
  $query
    ->condition('uid', $ids, 'IN');
  return $query;
}