You are here

function _drupalgap_add_term_data_to_entity in DrupalGap 7

Same name and namespace in other branches
  1. 7.2 drupalgap.module \_drupalgap_add_term_data_to_entity()
1 call to _drupalgap_add_term_data_to_entity()
_drupalgap_add_term_data_to_entities in ./drupalgap.module

File

./drupalgap.module, line 773
A module to provide a bridge between Drupal websites and PhoneGap mobile applications.

Code

function _drupalgap_add_term_data_to_entity($entity, $entity_type, $bundle) {

  // Grab the field info instances for this entity type.
  $instances = field_info_instances($entity_type, $bundle);

  // Let's add taxonomy term names along with their ids onto taxonomy term
  // reference fields.
  foreach ($instances as $field => $instance) {
    if ($instance['widget']['module'] == 'taxonomy' || $instance['widget']['module'] == 'options' && isset($instance['display']['default']['module']) && (isset($instance['display']['default']['module']) && ($instance['display']['default']['module'] == 'taxonomy' || $instance['display']['default']['module'] == 'i18n_taxonomy'))) {

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

      // Extract the term ids.
      $tids = array();
      if (isset($entity->{$field}[$language])) {
        foreach ($entity->{$field}[$language] as $delta => $term) {
          $tids[] = $term['tid'];
        }
      }
      if (empty($tids)) {
        continue;
      }

      // Grab the term names.
      $sql = "SELECT t.tid, t.name FROM {taxonomy_term_data} t WHERE t.tid IN (:tids)";
      $terms = db_query($sql, array(
        ':tids' => $tids,
      ))
        ->fetchAll();
      if (empty($terms)) {
        continue;
      }
      foreach ($entity->{$field}[$language] as $delta => $term) {
        foreach ($terms as $_term) {
          if ($term['tid'] == $_term->tid) {
            $entity->{$field}[$language][$delta]['name'] = $_term->name;
            break;
          }
        }
      }
    }
  }
  return $entity;
}