You are here

function drupalgap_entityreference_services_request_postprocess_alter in DrupalGap 7.2

Same name and namespace in other branches
  1. 7 modules/drupalgap_entityreference/drupalgap_entityreference.module \drupalgap_entityreference_services_request_postprocess_alter()

Implements hook_services_request_postprocess_alter().

File

modules/drupalgap_entityreference/drupalgap_entityreference.module, line 6

Code

function drupalgap_entityreference_services_request_postprocess_alter($controller, $args, &$result) {
  if ($controller['callback'] == '_node_resource_retrieve' || $controller['callback'] == '_user_resource_retrieve') {

    // Grab the field info instances for this entity type and bundle (if any).
    $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;
    }
    $instances = field_info_instances($entity_type, $bundle);

    // Add the target entity type, bundle, and title/name/subject/etc to the
    // result set.
    foreach ($instances as $field => $instance) {
      if (!isset($instance['display']['default']['module'])) {
        continue;
      }
      $module = $instance['display']['default']['module'];

      // Skip any fields not provided by the entity reference module (also
      // support the Organic Groups module here).
      if ($module != 'entityreference' && $module != 'og_ui') {
        continue;
      }

      // Load the field info, then extract the target entity type and bundle.
      $field_info = field_info_field($field);
      $entity_type = $field_info['settings']['target_type'];

      // Determine the field's language from the node, and fall back to
      // 'und' if the field's language isn't set.
      $language = $result->language;
      if (!isset($result->{$field}[$language])) {
        $language = 'und';
      }

      // Extract the target ids.
      $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;
      }

      // Depending on the entity type, determine the table name and fields
      // to grab from the DB.
      $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;
      }

      // Grab the data, then set them on the corresponding targets. Revert the
      // $column variable back to a single value in the special case of nodes.
      $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;
            break;
          }
        }
      }
    }
  }
}