You are here

public function ServicesEntityResourceController::field in Services Entity API 7.2

Implements ServicesResourceControllerInterface::field().

Overrides ServicesResourceControllerInterface::field

1 method overrides ServicesEntityResourceController::field()
ServicesEntityResourceControllerClean::field in plugins/services_entity_resource_clean.inc
Implements ServicesResourceControllerInterface::field().

File

plugins/services_entity_resource.inc, line 164

Class

ServicesEntityResourceController
Generic controller for entity-bases resources.

Code

public function field($entity_type, $entity_id, $field_name, $fields = '*', $raw = FALSE) {
  $entity = entity_load_single($entity_type, $entity_id);
  if (!$entity) {
    services_error('Entity not found', 404);
  }
  $wrapper = entity_metadata_wrapper($entity_type, $entity_id);
  if ($raw) {
    $return = $wrapper->{$field_name}
      ->raw();
  }
  else {
    $return = $wrapper->{$field_name}
      ->value();
  }
  $field = field_info_field($field_name);

  // Special handling for entityreference fields: run the new entities through
  // limit fields.
  if ($field['type'] == 'entityreference' && !$raw) {
    $entities = $return;
    $return = array();
    foreach ($entities as $id => $entity) {

      // The entity type here is the target type of the entityreference field.
      if (entity_access('view', $field['settings']['target_type'], $entity)) {
        $return[] = $this
          ->limit_fields($entity, $fields);
      }
    }
  }
  return $return;
}