You are here

protected function CiviEntityStorage::doLoadMultiple in CiviCRM Entity 8.3

Throws

\Drupal\Core\Entity\Sql\SqlContentEntityStorageException

Overrides SqlContentEntityStorage::doLoadMultiple

File

src/CiviEntityStorage.php, line 158

Class

CiviEntityStorage
Defines entity class for external CiviCRM entities.

Namespace

Drupal\civicrm_entity

Code

protected function doLoadMultiple(array $ids = NULL) {
  $entities = [];
  if ($ids === NULL) {
    $civicrm_entities = $this
      ->getCiviCrmApi()
      ->get($this->entityType
      ->get('civicrm_entity'));
    foreach ($civicrm_entities as $civicrm_entity) {
      $civicrm_entity = reset($civicrm_entity);
      $entity = $this
        ->prepareLoadedEntity($civicrm_entity);
      $entities[$entity
        ->id()] = $entity;
    }
  }

  // get all the fields
  $fields = $this
    ->getCiviCrmApi()
    ->getFields($this->entityType
    ->get('civicrm_entity'));
  $field_names = [];
  foreach ($fields as $field) {
    $field_names[] = $field['name'];
  }
  foreach ($ids as $id) {
    $options = [
      'id' => $id,
    ];
    $options['return'] = $field_names;
    if ($this->entityType
      ->get('civicrm_entity') === 'participant') {
      unset($options['return']);
    }
    $civicrm_entity = $this
      ->getCiviCrmApi()
      ->get($this->entityType
      ->get('civicrm_entity'), $options);
    $civicrm_entity = reset($civicrm_entity);
    if ($civicrm_entity) {
      if ($this->entityType
        ->get('civicrm_entity') === 'participant') {

        // Massage the values.
        $temporary = [];
        foreach ($civicrm_entity as $key => $value) {
          if (strpos($key, 'participant_') === 0) {
            $temporary[str_replace('participant_', '', $key)] = $value;
          }
          else {
            $temporary[$key] = $value;
          }
        }
        $civicrm_entity = $temporary;
      }
      $entity = $this
        ->prepareLoadedEntity($civicrm_entity);
      $entities[$entity
        ->id()] = $entity;
    }
  }
  return $entities;
}