You are here

private function ConnectionService::connectionQuery in RedHen CRM 8

Query for connections.

Parameters

\Drupal\Core\Entity\EntityInterface $entity: The entity we're querying against.

\Drupal\Core\Entity\EntityInterface $entity2: The second entity we're querying against.

string $connection_type: Limit query to this connection type.

bool $active: Only active connections.

Return value

array An array of matches.

2 calls to ConnectionService::connectionQuery()
ConnectionService::getConnectionCount in modules/redhen_connection/src/ConnectionService.php
Returns the number of connections to this entity.
ConnectionService::getConnections in modules/redhen_connection/src/ConnectionService.php
Returns the connections to this entity.

File

modules/redhen_connection/src/ConnectionService.php, line 241

Class

ConnectionService
Provides an interface for getting connections between entities.

Namespace

Drupal\redhen_connection

Code

private function connectionQuery(EntityInterface $entity, EntityInterface $entity2 = NULL, $connection_type = NULL, $active = TRUE) {
  $entity_type = $entity
    ->getEntityType()
    ->id();
  $entity2_type = $entity2 ? $entity2
    ->getEntityType()
    ->id() : NULL;
  $connections_matches = [];
  $potential_endpoints = [];
  if (!$connection_type) {
    $connection_types = $this
      ->getConnectionTypes($entity, $entity2);
  }
  else {
    $connection_types = [
      ConnectionType::load($connection_type),
    ];
  }
  if (!empty($connection_types)) {
    foreach ($connection_types as $type) {
      if ($endpoint_fields = $type
        ->getEndpointFields($entity_type)) {
        $potential_endpoints[$type
          ->id()]['entity1'] = $endpoint_fields;
      }
      if ($entity2_type) {
        if ($endpoint2_fields = $type
          ->getEndpointFields($entity2_type)) {
          $potential_endpoints[$type
            ->id()]['entity2'] = $endpoint2_fields;
        }
      }
    }
    $database = \Drupal::database();
    foreach ($potential_endpoints as $connection_type => $endpoint_group) {
      $query = $database
        ->select('redhen_connection', 'rc')
        ->fields('rc', [
        'id',
      ])
        ->condition('type', $connection_type);
      if ($active) {
        $query
          ->condition('status', $active);
      }

      // Parent condition group.
      $entityAndGroup = $query
        ->andConditionGroup();

      // Entity 1 Group.
      $entity1Group = $query
        ->orConditionGroup();
      $entity1Group
        ->condition($endpoint_group['entity1'][0], $entity
        ->id());

      // If there are multiple potential endpoints that match entity 1 type.
      if (count($endpoint_group['entity1']) > 1) {
        $additional_entities = array_slice($endpoint_group['entity1'], 1, 1, FALSE);
        $entity1Group
          ->condition($additional_entities[0], $entity
          ->id());
      }
      $entityAndGroup
        ->condition($entity1Group);

      // Entity 2 Group.
      if (isset($endpoint_group['entity2'])) {
        $entity2Group = $query
          ->orConditionGroup()
          ->condition($endpoint_group['entity2'][0], $entity2
          ->id());

        // If there are multiple potential endpoints that match entity 2 type.
        if (isset($endpoint_group['entity2'][1])) {
          $entity2Group
            ->condition($endpoint_group['entity2'][1], $entity2
            ->id());
        }
        $entityAndGroup
          ->condition($entity2Group);
      }
      $query
        ->condition($entityAndGroup);
      $results = $query
        ->execute()
        ->fetchCol();

      // If there are matched results merge them into the result set.
      if ($results) {
        $connections_matches = array_unique(array_merge($connections_matches, $results));
      }
    }
  }
  return $connections_matches;
}