public function remote__data::load

3.x remote__data.inc public remote__data::load($entity)

Overrides WebServicesField::load

See also

WebServicesField::load()

File

tripal_ws/includes/TripalFields/remote__data/remote__data.inc, line 130

Class

remote__data
@class Purpose:

Code

public function load($entity) {

  $field_name = $this->field['field_name'];
  $field_type = $this->field['type'];

  // Set some defaults for the empty record.
  $entity->{$field_name}['und'][0] = array(
    'value' => array(),
    'remote_entity' => array(),
  );

  // If this field is being loaded via web services then just return.
  if ($this->loaded_via_ws == TRUE) {
    return;
  }

  // Get the query set by the admin for this field and replace any tokesn
  $query_str = $this->instance['settings']['data_info']['query'];
  $bundle = tripal_load_bundle_entity(array('name' => $entity->bundle));
  $query_str = tripal_replace_entity_tokens($query_str, $entity, $bundle);

  // Make the request.
  $data = $this->makeRemoteRequest($query_str);
  if (!$data) {
    return;
  }

  $total_items = $data['totalItems'];

  // Iterate through the members returned and save those for the field.
  for ($i = 0; $i < count($data['members']); $i++) {
    $member = $data['members'][$i];

    // Get the cotent type and remote entity id
    $content_type = $member['@type'];
    $remote_entity_id = $member['@id'];
    $remote_entity_id = preg_replace('/^.*\/(\d+)/', '$1', $remote_entity_id);

    // Save the member information for use later.
    $entity->{$field_name}['und'][$i]['remote_entity'] = $member;

    // Separate the query_field if it has subfields.
    $rd_field_name = $this->instance['settings']['data_info']['rd_field_name'];
    $subfields = explode(',', $rd_field_name);
    $query_field = $subfields[0];

    // Next get the the details about this member.
    $query_field_url = $content_type . '/' . $remote_entity_id . '/' . $query_field;
    $field_data = $this->makeRemoteRequest($query_field_url);
    if (!$field_data) {
      // If we encounter any type of error, we'll reset the field and return.
      $entity->{$field_name}['und'] = array();
      $entity->{$field_name}['und'][0] = array(
        'value' => array(),
        'remote_entity' => array(),
      );
      return;
    }

    // Set the field data as the value.
    $field_data_type = $field_data['@type'];
    $entity->{$field_name}['und'][$i]['value'] = $field_data;
  }
}