You are here

drupalgap_entityreference.module in DrupalGap 7

File

modules/drupalgap_entityreference/drupalgap_entityreference.module
View source
<?php

/**
 * Implements hook_services_request_postprocess_alter().
 */
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',
  ))) {

    // 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;
      case '_taxonomy_term_retrieve':
        $entity_type = 'taxonomy_term';
        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']) && !isset($instance['display']['drupalgap']['module'])) {
        continue;
      }
      $module = isset($instance['display']['drupalgap']['module']) ? $instance['display']['drupalgap']['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);
      if (!isset($field_info['settings']['target_type'])) {
        continue;
      }
      $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;
      }

      // Pull out the display object, use the DrupalGap display mode if it is
      // set, otherwise fallback to the Default display mode.
      $display = isset($instance['display']['drupalgap']) ? $instance['display']['drupalgap'] : $instance['display']['default'];

      // 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;

            // If the display mode is set to "Rendered entity", then load up
            // the entity and attach it to the result.
            if ($display['type'] == 'entityreference_entity_view') {
              $result->{$field}[$language][$delta]['entity'] = call_user_func($entity_type . '_load', $target->{$primary_key});
            }
            break;
          }
        }
      }
    }
  }
}