You are here

protected function CivicrmEntityController::loadEntities in CiviCRM Entity 7.2

Load entities to an array.

Parameters

array $condition:

Return value

mixed

Throws

\CiviCRM_API3_Exception

1 call to CivicrmEntityController::loadEntities()
CivicrmEntityController::load in ./civicrm_entity_controller.inc
Implements DrupalEntityControllerInterface::load().

File

./civicrm_entity_controller.inc, line 308

Class

CivicrmEntityController
Entity Controller for CiviCRM entities

Code

protected function loadEntities($condition) {
  $entities = array();
  $fields = civicrm_api3($this->entityInfo['description'], 'getfields', array(
    'action' => 'create',
  ));
  if (!empty($fields['values'])) {
    $return_fields['return'] = array_keys($fields['values']);
  }

  // some fields are still not retrieved, for example the financial_type_id for Contribution get even though they are returned in the getfields list
  // and if you don't use any return conditions, they come through, but in that case custom fields don't come through
  // typical inconsistencies in the CiviCRm api that play havoc for automation
  // we really would prefer to use the 'create' action in the getfields list, because you get more....
  // but we need get to return all the field values for all the fields that are available for writing in the 'create' action
  // so we make 2 api calls, one with the 'return' property, and one without
  // get what we get with a API call with no 'return' property
  $civicrm_entities = civicrm_api3($this->entityInfo['description'], 'get', $condition);

  // get what we get by the 'return' property in the API call
  if (!empty($return_fields['return'])) {
    $condition['return'] = $return_fields['return'];
    $civicrm_entities2 = civicrm_api3($this->entityInfo['description'], 'get', $condition);
  }

  // merge the two together
  $new_civicrm_entities = array();
  foreach ($civicrm_entities['values'] as $id => $entity) {
    if (!empty($civicrm_entities2['values'][$id])) {
      $new_civicrm_entities[$id] = array_merge($entity, $civicrm_entities2['values'][$id]);
    }
    else {
      $new_civicrm_entities[$id] = $entity;
    }

    // unset anything not in the return array (if you wanted to) but why?

    /*foreach($new_civicrm_entities[$id] as $key => $value){
        if(!in_array($key, $return_fields['return'])) {
          unset($new_civicrm_entities[$id][$key]);
        }
      }*/
  }

  // create the Drupal Entities
  if (count($new_civicrm_entities)) {
    foreach ($new_civicrm_entities as $id => $civicrm_entity) {

      // @TODO improve this casting.
      $entities[$id] = new CivicrmEntity($civicrm_entity, $this->entityType);
      if ($this->entityType == 'civicrm_contribution') {
        $entities[$id]->source = $entities[$id]->contribution_source;
      }
    }
    return $entities;
  }
  return $entities;
}