You are here

function _services_entity_resource_retrieve in Services Entity API 7

1 string reference to '_services_entity_resource_retrieve'
services_entity_services_resources in ./services_entity.services.inc
Implementation of hook_services_resources().

File

./services_entity.resources.inc, line 30

Code

function _services_entity_resource_retrieve($entity_type, $entity_key, $fields) {

  // Build an EFQ to retrieve the entity. This also satisfies access control.
  $query = new EntityFieldQuery();
  $query
    ->entityCondition('entity_type', $entity_type)
    ->addTag('service_entity_resource')
    ->addMetaData('entity_type', $entity_type);

  // If there's no : in the entity key, we're looking for the id.
  if (strpos($entity_key, ':') == 0) {
    $query
      ->entityCondition('entity_id', $entity_key);
  }
  else {

    // First split the property key and the value.
    list($key, $value) = explode(':', $entity_key);

    // We need to be sure that is a unique property to avoid multiple results
    // index option should be used instead.
    $info = entity_get_info($entity_type);
    $schema = drupal_get_schema($info['base table']);
    if (!empty($schema['unique keys'][$key])) {
      $query
        ->propertyCondition($key, $value);
    }
    else {

      // If this is not a unique property, avoid doing the query.
      return services_error(t('Loading an entity using a non unique property is not allowed.'), 404);
    }
  }
  $entities = $query
    ->execute();
  if (!empty($query->ordered_results)) {
    $result = _services_postprocess_entities($entities, $query->ordered_results, $fields);
    return reset($result);
  }
  else {
    return services_error(t('No entities retrieved.'), 404);
  }
}