function tripal_load_remote_entity
3.x tripal_ws.api.inc | tripal_load_remote_entity($remote_entity_id, $site_id, $bundle_accession, $field_ids) |
Queries a remote site for an entity.
This function returns a "fake" entity containing values for all fields specified.
Parameters
$remote_entity_id: A remote entity ID.
$site_id: The numeric site ID for the remote Tripal site.
$bundle_accession: The controlled vocabulary term accession for the content type on the remote Tripal site.
$field_ids: The controlled vocabulary term accessions for the fields available on the remote content type. Any remote fields that matches these IDs will be added to the entity returned.
Return value
A fake entity object.
Related topics
File
- tripal_ws/
api/ tripal_ws.api.inc, line 561 - This file provides the Tripal Web Services API: a set of functions for interacting with the Tripal Web Services.
Code
function tripal_load_remote_entity($remote_entity_id, $site_id, $bundle_accession, $field_ids) {
// Get the site documentation (loads from cache if already retrieved).
$site_doc = tripal_get_remote_API_doc($site_id);
// Get the remote entity and create the fake entity.
$remote_entity = tripal_get_remote_content($site_id, $bundle_accession . '/' . $remote_entity_id);
if (!$remote_entity) {
return FALSE;
}
// Start building the fake entity.
$entity = new stdClass();
$entity->entityType = 'TripalEntity';
$entity->entityInfo =[];
$entity->id = $remote_entity_id;
$entity->type = 'TripalEntity';
$entity->bundle = $bundle_accession;
$entity->site_id = $site_id;
// Get the context JSON for this remote entity, we'll use it to map
$context_url = $remote_entity['@context'];
$context = tripal_get_remote_content_context($site_id, $context_url, $bundle_accession);
if (!$context) {
return $entity;
}
// Iterate through the fields and the those values to the entity.
foreach ($field_ids as $field_id) {
$field = tripal_get_remote_field_info($site_id, $bundle_accession, $field_id);
$instance = tripal_get_remote_field_instance_info($site_id, $bundle_accession, $field_id);
$field_name = $field['field_name'];
$field_key = '';
foreach ($context as $k => $v) {
if (!is_array($v) and $v == $field_id) {
$field_key = $k;
}
}
// If the field is not in this remote bundle then add an empty value.
if (!$field_key) {
$entity->{$field_name}['und'][0]['value'] = '';
continue;
}
if (!array_key_exists($field_key, $remote_entity)) {
$entity->{$field_name}['und'][0]['value'] = '';
continue;
}
// If the key is for a field that is not "auto attached' then we need
// to get that field through a separate call.
$attached = TRUE;
if (array_key_exists($field_id, $context) and is_array($context[$field_id]) and
array_key_exists('@type', $context[$field_id]) and $context[$field_id]['@type'] == '@id') {
$attached = FALSE;
}
// Set the value for this field.
$value = '';
if (is_array($remote_entity[$field_key])) {
$value = _tripal_update_remote_entity_field($remote_entity[$field_key], $context, 1);
}
else {
$value = $remote_entity[$field_key];
}
// If the field is not attached then we have to query another level.
if (!$attached) {
$field_data = drupal_http_request($value);
if (!$field_data) {
tripal_report_error('tripal_ws', TRIPAL_ERROR,
'There was a poblem retrieving the unattached field, "!field:", for the remote entity: !entity_id.',
array('!field' => $field_id, '!entity_id' => $remote_entity_id));
$value = '';
}
$field_data = drupal_json_decode($field_data->data);
// Get the context for this field so we can map the keys to the
// controlled vocabulary accessions. If it fails then skip this field.
$field_context_url = $field_data['@context'];
$field_context = tripal_get_remote_content_context($site_id, $field_context_url, $bundle_accession, $field_id);
if (!$field_context) {
continue;
}
$value = _tripal_update_remote_entity_field($field_data, $field_context);
}
$entity->{$field_name}['und'][0]['value'] = $value;
}
return $entity;
}