You are here

protected function TeamMembersSelection::buildEntityQuery in Apigee Edge 8

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

1 call to TeamMembersSelection::buildEntityQuery()
TeamMembersSelection::getReferenceableEntities in modules/apigee_edge_teams/src/Plugin/EntityReferenceSelection/TeamMembersSelection.php
Gets the list of referenceable entities.

File

modules/apigee_edge_teams/src/Plugin/EntityReferenceSelection/TeamMembersSelection.php, line 171

Class

TeamMembersSelection
Provides a team member entity selection plugin.

Namespace

Drupal\apigee_edge_teams\Plugin\EntityReferenceSelection

Code

protected function buildEntityQuery($match = NULL, $match_operator = 'CONTAINS') {

  // Do not pass $match so that it does not add the "name" field to the query,
  // as we will search by email instead.
  $query = parent::buildEntityQuery(NULL, $match_operator);
  $match = trim($match);
  if ($match) {
    $query
      ->condition('mail', $match, $match_operator);
  }
  $config = $this
    ->getConfiguration();
  $team_name = $config['filter']['team'];

  // Exclude those users from the list who does not have a developer
  // account on Apigee Edge.
  // ("Add or Update Company Developers" API call would fail anyway if the
  // list of new members would contain any email address that does not
  // belong to an existing developer.)
  try {
    $existing_developers = $this->developerController
      ->getEntityIds();
  } catch (\Exception $exception) {

    // If list of existing developers (email addresses) could not be
    // retrieved then return an empty list.
    $query
      ->condition('mail', 0);
    $context = Error::decodeException($exception);
    $this->logger
      ->error("Unable to retrieve list of developer email addresses from Apigee Edge. @message %function (line %line of %file). <pre>@backtrace_string</pre>", $context);
    return $query;
  }
  if (empty($existing_developers)) {

    // If list of existing developers is empty then return an empty list
    // too. (Developers should be synced.)
    $query
      ->condition('mail', 0);
  }
  else {
    $query
      ->condition('mail', $existing_developers, 'IN');
  }

  // Do not display users who are already member of the team.
  if ($team_name) {
    try {
      $team_members = $this->teamMembershipManager
        ->getMembers($team_name);
      if (!empty($team_members)) {
        $query
          ->condition('mail', $team_members, 'NOT IN');
      }
    } catch (\Exception $exception) {

      // If team members could not be retrieved return an empty list.
      $query
        ->condition('mail', 0);
      $context = [
        '%team' => $team_name,
      ];
      $context += Error::decodeException($exception);
      $this->logger
        ->error("Unable to retrieve list of %team team from Apigee Edge. @message %function (line %line of %file). <pre>@backtrace_string</pre>", $context);
    }
  }
  return $query;
}