commit 3223ad81a09157a3e91229d0aef2fede9a0f1653
Author: Florian Weber <florian@webflo.org>
Date:   Wed Nov 30 01:12:17 2011 +0100

    Issue #1356434: Use original taxonomy fields.

diff --git a/i18n_taxonomy/i18n_taxonomy.install b/i18n_taxonomy/i18n_taxonomy.install
index 5edb6d4..ee1a0d6 100644
--- a/i18n_taxonomy/i18n_taxonomy.install
+++ b/i18n_taxonomy/i18n_taxonomy.install
@@ -6,14 +6,6 @@
  */
 
 /**
- * Implements hook_field_schema().
- */
-function i18n_taxonomy_field_schema($field) {
-  module_load_install('taxonomy');
-  return taxonomy_field_schema($field);
-}
-
-/**
  * Set language field in its own table.
  * Do not drop node.language now, just in case.
  * TO-DO: Drop old tables, fields
@@ -55,13 +47,9 @@ function i18n_taxonomy_schema_alter(&$schema) {
  * Implements hook_enable()
  */
 function i18n_taxonomy_enable() {
-  // Change module assignment for fields in {field_config} table
-  // from 'taxonomy' to 'i18n_taxonomy', so i18n_taxonomy.module is
-  // handling all term reference fields
-  // (added to solve issue http://drupal.org/node/1078422)
   foreach (field_info_fields() as $fieldname => $field) {
-    if (($field['type'] == 'taxonomy_term_reference') && ($field['module'] == 'taxonomy') && ($field['deleted'] == 0)) {
-      $field['module'] = 'i18n_taxonomy';
+    if ($field['type'] == 'taxonomy_term_reference') {
+      $field['settings']['options_list_callback'] = 'i18n_taxonomy_allowed_values';
       field_update_field($field);
     }
   }
@@ -71,14 +59,9 @@ function i18n_taxonomy_enable() {
  * Implements hook_disable()
  */
 function i18n_taxonomy_disable() {
-  // Change module assignment for fields in {field_config} table
-  // from 'i18n_taxonomy' back to 'taxonomy', so taxonomy.module is
-  // handling all term reference fields again, and no such field is
-  // de-activated when i18n_taxonomy.module is disabled.
-  // (added to solve issue http://drupal.org/node/1078422)
   foreach (field_info_fields() as $fieldname => $field) {
-    if (($field['type'] == 'taxonomy_term_reference') && ($field['module'] == 'i18n_taxonomy') && ($field['deleted'] == 0)) {
-      $field['module'] = 'taxonomy';
+    if ($field['type'] == 'taxonomy_term_reference') {
+      unset($field['settings']['options_list_callback']);
       field_update_field($field);
     }
   }
@@ -126,4 +109,17 @@ function i18n_taxonomy_update_7001() {
   if (db_field_exists('taxonomy_term_data', 'trid')) {
     db_drop_field('taxonomy_term_data', 'trid');
   }
-}
\ No newline at end of file
+}
+
+/**
+ * Switch back to real taxonomy fields and override option_list_callback.
+ */
+function i18n_taxonomy_update_7002(&$sandbox) {
+  foreach (field_info_fields() as $fieldname => $field) {
+    if ($field['type'] == 'taxonomy_term_reference') {
+      $field['module'] = 'taxonomy';
+      $field['settings']['options_list_callback'] = 'i18n_taxonomy_allowed_values';
+      field_update_field($field);
+    }
+  }
+}
diff --git a/i18n_taxonomy/i18n_taxonomy.module b/i18n_taxonomy/i18n_taxonomy.module
index 696da95..28485eb 100644
--- a/i18n_taxonomy/i18n_taxonomy.module
+++ b/i18n_taxonomy/i18n_taxonomy.module
@@ -184,16 +184,6 @@ function i18n_taxonomy_field_formatter_info() {
 }
 
 /**
- * Implements hook_field_formatter_prepare_view().
- *
- * This preloads all taxonomy terms for multiple loaded objects at once and
- * unsets values for invalid terms that do not exist.
- */
-function i18n_taxonomy_field_formatter_prepare_view($entity_type, $entities, $field, $instances, $langcode, &$items, $displays) {
-  return taxonomy_field_formatter_prepare_view($entity_type, $entities, $field, $instances, $langcode, $items, $displays);
-}
-
-/**
  * Implements hook_field_formatter_view().
  */
 function i18n_taxonomy_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display) {
@@ -272,9 +262,10 @@ function i18n_taxonomy_field_attach_view_alter(&$output, $context) {
 function i18n_taxonomy_field_info_alter(&$info) {
   // Change default formatter for term reference fields
   $info['taxonomy_term_reference']['default_formatter'] = 'i18n_taxonomy_term_reference_link';
-  // Change module for this field. Basically we take over this field and this means
-  // we need to implement a bunch of hooks for fields and options, including hook_field_schema()
-  $info['taxonomy_term_reference']['module'] = 'i18n_taxonomy';
+
+  // Translate field values.
+  $info['taxonomy_term_reference']['options_list_callback'] = 'i18n_taxonomy_allowed_values';
+
   // Sync callback for field translations
   $info['taxonomy_term_reference']['i18n_sync_callback'] = 'i18n_taxonomy_field_prepare_translation';
 }
@@ -287,94 +278,48 @@ function i18n_taxonomy_field_info_alter(&$info) {
  * @param array $field
  *   The field record just read from the database.
  */
-function i18n_taxonomy_field_storage_details_alter($details, &$field, $instance) {
+function i18n_taxonomy_field_storage_details_alter(&$details, &$field) {
   if ($field['type'] === 'taxonomy_term_reference') {
-    // We also take over existing taxonomy_term_reference fields.
-    $field['module'] = 'i18n_taxonomy';
-  }
-}
-
-/**
- * Implements hook_field_prepare_translation().
- * 
- * Prepare and synchronize translation for term reference fields
- */
-function i18n_taxonomy_field_prepare_translation($entity_type, $entity, $field, $instance, $langcode, &$items, $source_entity, $source_langcode) {
-  foreach ($items as $index => $item) {
-    $term = isset($item['taxonomy_term']) ? $item['taxonomy_term'] : taxonomy_term_load($item['tid']);
-    if ($translation = i18n_taxonomy_term_get_translation($term, $langcode)) {
-      $items[$index] = array(
-        'taxonomy_term' => $translation,
-        'tid' => $translation->tid
-      );
-    }
+    $field['settings']['options_list_callback'] = 'i18n_taxonomy_allowed_values';
   }
 }
 
 /**
- * Implements hook_field_is_empty().
- */
-function i18n_taxonomy_field_is_empty($item, $field) {
-  return taxonomy_field_is_empty($item, $field);
-}
-
-/**
- * Implements hook_field_widget_error().
- */
-function i18n_taxonomy_field_widget_error($element, $error, $form, &$form_state) {
-  form_error($element, $error['message']);
-}
-
-/**
- * Implements hook_field_widget_form().
- */
-function i18n_taxonomy_field_widget_form(&$form, &$form_state, $field, $instance, $langcode, $items, $delta, $element) {
-  return taxonomy_field_widget_form($form, $form_state, $field, $instance, $langcode, $items, $delta, $element);
-}
-
-/**
- * Implements hook_field_settings_form().
- */
-function i18n_taxonomy_field_settings_form($field, $instance, $has_data) {
-  return taxonomy_field_settings_form($field, $instance, $has_data);
-}
-
-/**
- * Implements hook_field_validate().
- */
-function i18n_taxonomy_field_validate($entity_type, $entity, $field, $instance, $langcode, $items, &$errors) {
-  return taxonomy_field_validate($entity_type, $entity, $field, $instance, $langcode, $items, $errors);
-}
+ * Implements hook_field_attach_prepare_translation_alter().
 
-/**
- * Implements hook_field_presave().
- *
- * Create any new terms defined in a freetagging vocabulary.
- */
-function i18n_taxonomy_field_presave($entity_type, $entity, $field, $instance, $langcode, &$items) {
-  return taxonomy_field_presave($entity_type, $entity, $field, $instance, $langcode, $items);
-}
-
-/**
- * Implements hook_field_insert().
+ * Prepare and synchronize translation for term reference fields.
  */
-function i18n_taxonomy_field_insert($entity_type, $entity, $field, $instance, $langcode, &$items) {
-  return taxonomy_field_insert($entity_type, $entity, $field, $instance, $langcode, $items);
-}
+function i18n_taxonomy_field_attach_prepare_translation_alter(&$entity, $context) {
+  $entity_type = $context['entity_type'];
 
-/**
- * Implements hook_field_update().
- */
-function i18n_taxonomy_field_update($entity_type, $entity, $field, $instance, $langcode, &$items) {
-  return taxonomy_field_update($entity_type, $entity, $field, $instance, $langcode, $items);
-}
+  $options = array(
+    'default' => FALSE,
+    'deleted' => FALSE,
+    'language' => NULL,
+  );
 
-/**
- * Implements hook_options_list().
- */
-function i18n_taxonomy_options_list($field) {
-  $function = !empty($field['settings']['options_list_callback']) ? $field['settings']['options_list_callback'] : 'i18n_taxonomy_allowed_values';
-  return $function($field);
+  // Determine the list of instances to iterate on.
+  list(, , $bundle) = entity_extract_ids($entity_type, $context['source_entity']);
+  $instances = _field_invoke_get_instances($entity_type, $bundle, $options);
+  if (!empty($instances)) {
+    foreach($instances as $field_info) {
+      $field = field_info_field($field_info['field_name']);
+      if ($field['type'] == 'taxonomy_term_reference' && isset($entity->{$field_info['field_name']})) {
+        // iterate over languages.
+        foreach($entity->{$field_info['field_name']} as $language => &$items) {
+          foreach ($items as $index => $item) {
+            $term = isset($item['taxonomy_term']) ? $item['taxonomy_term'] : taxonomy_term_load($item['tid']);
+            if ($translation = i18n_taxonomy_term_get_translation($term, $entity->language)) {
+              $items[$index] = array(
+                'taxonomy_term' => $translation,
+                'tid' => $translation->tid
+              );
+            }
+          }
+        }
+      }
+    }
+  }
 }
 
 /**
