private function TripalContentService_v0_1::addEntityField

3.x TripalContentService_v0_1.inc private TripalContentService_v0_1::addEntityField($resource, $term, $entity, $bundle, $field, $instance, $service_path, $expfield = NULL)

Adds the field as a property of the entity resource.

3 calls to TripalContentService_v0_1::addEntityField()
TripalContentService_v0_1::addEntityFields in tripal_ws/includes/TripalWebService/TripalContentService_v0_1.inc
Adds the fields as properties of an entity resource.
TripalContentService_v0_1::doEntityList in tripal_ws/includes/TripalWebService/TripalContentService_v0_1.inc
Creates a collection of resources for a given type.
TripalContentService_v0_1::doExpandedField in tripal_ws/includes/TripalWebService/TripalContentService_v0_1.inc
Creates a resource for an expanded field of an entity.

File

tripal_ws/includes/TripalWebService/TripalContentService_v0_1.inc, line 329

Class

TripalContentService_v0_1

Code

private function addEntityField($resource, $term, $entity, $bundle, $field, $instance, 
$service_path, $expfield = NULL) {

  // If the entity is set to hide fields that have no values then we
  // want to honor that in the web services too.
  $hide_fields = tripal_get_bundle_variable('hide_empty_field', $bundle->id, 'hide');

  // Get the field  settings.
  $field_name = $field['field_name'];
  $field_settings = $field['settings'];

  $items = field_get_items('TripalEntity', $entity, $field_name);
  if (!$items) {
    return;
  }

  // Give modules the opportunity to edit values for web services. This hook
  // really should be used sparingly. Where it helps is with non Tripal fields
  // that are added to a TripalEntity content type and it doesn't follow
  // the rules (e.g. Image field).
  drupal_alter('tripal_ws_value', $items, $field, $instance);

  $values = array();
  for ($i = 0; $i < count($items); $i++) {
    if (array_key_exists('value', $items[$i])) {
      $values[$i] = $this->sanitizeFieldKeys($resource, $items[$i]['value'], $bundle, $service_path);
    }
    elseif ($field['type'] == 'image') {
      $url = file_create_url($items[$i]['uri']);
      $values[$i] = $this->sanitizeFieldKeys($resource, $url, $bundle, $service_path);
    }
    else {
      // TODO: handle this case.
    }
  }

  if ($hide_fields == 'hide' and empty($values[0])) {
    return;
  }

  // If the field cardinality is 1
  if ($field['cardinality'] == 1) {

    // If the value is an array and this is the field page then all of those
    // key/value pairs should be added directly to the response.
    if (is_array($values[0])) {
      if ($expfield) {
        foreach ($values[0] as $k => $v) {
          $resource->addProperty($k, $v);
        }
      }
      else {
        $this->addResourceProperty($resource, $term, $values[0], array('lowercase', 'spacing'));
      }
    }
    // If the value is not an array it's a scalar so add it as is to the
    // response.
    else {
      $this->addResourceProperty($resource, $term, $values[0], array('lowercase', 'spacing'));
    }
  }

  // If the field cardinality is > 1 or -1 (for unlimited)
  if ($field['cardinality'] != 1) {

    // If this is the expanded field page then we need to swap out
    // the resource for a collection.
    $response = new TripalWebServiceCollection($service_path . '/' . urlencode($expfield), $this->params);
    $label = tripal_get_term_details('rdfs', 'label');
    $this->addResourceProperty($response, $label, $instance['label']);
    $i = 0;
    foreach ($values as $delta => $element) {
      $member = new TripalWebServiceResource($service_path . '/' . urlencode($expfield));
      $member->setID($i);
      // Add the context of the parent resource because all of the keys
      // were santizied and set to match the proper context.
      $member->setContext($resource);
      $this->setResourceType($member, $term);
      foreach ($element as $key => $value) {
        $member->addProperty($key, $value);
      }
      $response->addMember($member);
      $i++;
    }
    if ($expfield) {
      $resource = $response;
    }
    else {
      //$this->resource->addProperty($key, $response);
      $this->addResourceProperty($resource, $term, $response, array('lowercase', 'spacing'));
    }
  }
}