drupalgap_entityreference.module in DrupalGap 7
File
modules/drupalgap_entityreference/drupalgap_entityreference.module
View source
<?php
function drupalgap_entityreference_services_request_postprocess_alter($controller, $args, &$result) {
if (in_array($controller['callback'], array(
'_node_resource_retrieve',
'_taxonomy_term_retrieve',
'_user_resource_retrieve',
))) {
$entity_type = null;
$bundle = null;
switch ($controller['callback']) {
case '_node_resource_retrieve':
$entity_type = 'node';
$bundle = $result->type;
break;
case '_user_resource_retrieve':
$entity_type = 'user';
break;
case '_taxonomy_term_retrieve':
$entity_type = 'taxonomy_term';
break;
}
$instances = field_info_instances($entity_type, $bundle);
foreach ($instances as $field => $instance) {
if (!isset($instance['display']['default']['module']) && !isset($instance['display']['drupalgap']['module'])) {
continue;
}
$module = isset($instance['display']['drupalgap']['module']) ? $instance['display']['drupalgap']['module'] : $instance['display']['default']['module'];
if ($module != 'entityreference' && $module != 'og_ui') {
continue;
}
$field_info = field_info_field($field);
if (!isset($field_info['settings']['target_type'])) {
continue;
}
$entity_type = $field_info['settings']['target_type'];
$language = $result->language;
if (!isset($result->{$field}[$language])) {
$language = 'und';
}
$target_ids = array();
if (isset($result->{$field}[$language])) {
foreach ($result->{$field}[$language] as $delta => $reference) {
$target_ids[] = $reference['target_id'];
}
}
if (empty($target_ids)) {
continue;
}
$display = isset($instance['display']['drupalgap']) ? $instance['display']['drupalgap'] : $instance['display']['default'];
$table = null;
$column = null;
$primary_key = null;
switch ($entity_type) {
case 'comment':
$table = 'comment';
$column = 'subject';
$primary_key = 'cid';
break;
case 'file':
$table = 'file_managed';
$column = 'filename';
$primary_key = 'fid';
break;
case 'node':
$table = 'node';
$column = 'title, type';
$primary_key = 'nid';
break;
case 'taxonomy_term':
$table = 'taxonomy_term_data';
$column = 'name';
$primary_key = 'tid';
break;
case 'taxonomy_vocabulary':
$table = 'taxonomy_vocabulary';
$column = 'name';
$primary_key = 'vid';
break;
case 'user':
$table = 'users';
$column = 'name';
$primary_key = 'uid';
break;
}
if (!isset($table) || !isset($column) || !isset($primary_key)) {
continue;
}
$sql = "SELECT {$column}, {$primary_key} FROM {" . $table . "} WHERE {$primary_key} IN (:target_ids)";
$targets = db_query($sql, array(
':target_ids' => $target_ids,
))
->fetchAll();
if ($column == 'title, type') {
$column = 'title';
}
foreach ($result->{$field}[$language] as $delta => $reference) {
foreach ($targets as $target) {
if ($reference['target_id'] == $target->{$primary_key}) {
$result->{$field}[$language][$delta][$column] = $target->{$column};
$result->{$field}[$language][$delta]['entity_type'] = $entity_type;
$result->{$field}[$language][$delta]['bundle'] = isset($reference->type) ? $reference->type : null;
if ($display['type'] == 'entityreference_entity_view') {
$result->{$field}[$language][$delta]['entity'] = call_user_func($entity_type . '_load', $target->{$primary_key});
}
break;
}
}
}
}
}
}