You are here

public function Query::execute in CiviCRM Entity 8.3

Execute the query.

Return value

int|array Returns an integer for count queries or an array of ids. The values of the array are always entity ids. The keys will be revision ids if the entity supports revision and entity ids if not.

Overrides QueryInterface::execute

File

src/Entity/Query/CiviCRM/Query.php, line 28

Class

Query
The CiviCRM entity query class.

Namespace

Drupal\civicrm_entity\Entity\Query\CiviCRM

Code

public function execute() {
  $params = [];
  foreach ($this->condition
    ->conditions() as $condition) {

    // If there's anything requiring a custom field, set condition which cannot
    // be completed.
    // @todo Introduced when supporting field config. Find something better.
    // @see \Drupal\field_ui\Form\FieldStorageConfigEditForm::validateCardinality()
    if (substr($condition['field'], 0, 6) === 'field_') {
      $params['id'] = '-1';
      break;
    }
    $operator = $condition['operator'] ?: '=';
    if ($operator == 'CONTAINS') {
      $params[$condition['field']] = [
        'LIKE' => '%' . $condition['value'] . '%',
      ];
    }
    elseif ($operator != '=') {
      $params[$condition['field']] = [
        $operator => $condition['value'],
      ];
    }
    else {
      $params[$condition['field']] = $condition['value'];
    }
  }
  $this
    ->initializePager();
  if ($this->range) {
    $params['options'] = [
      'limit' => $this->range['length'],
      'offset' => $this->range['start'],
    ];
  }
  if ($this->count) {
    return $this->civicrmApi
      ->getCount($this->entityType
      ->get('civicrm_entity'), $params);
  }
  else {
    $result = $this->civicrmApi
      ->get($this->entityType
      ->get('civicrm_entity'), $params);
    return array_keys($result);
  }
}