From 6c79a00faaceb1ba00e1511784fd663d99dacda3 Mon Sep 17 00:00:00 2001
From: Kristiaan Van den Eynde <magentix@gmail.com>
Date: Wed, 29 Jan 2014 10:08:33 +0100
Subject: [PATCH] Issue #2158057 by kristiaanvandeneynde, rogical: UUID
 support for default value in taxonomy term reference field
 settings.

---
 includes/modules/taxonomy.inc   |  104 +++++++++++++++++++++++++--------------
 includes/uuid_bean.features.inc |    4 --
 includes/uuid_node.features.inc |    4 --
 uuid_features.module            |    9 ++++
 4 files changed, 77 insertions(+), 44 deletions(-)

diff --git a/includes/modules/taxonomy.inc b/includes/modules/taxonomy.inc
index d7fdd7e..0659747 100644
--- a/includes/modules/taxonomy.inc
+++ b/includes/modules/taxonomy.inc
@@ -1,61 +1,93 @@
 <?php
 /**
  * @file
- * uuid_node hooks on behalf of the taxonomy module.
+ * field_instance features component hooks on behalf of the taxonomy module.
  */
 
 /**
- * Implements hook_uuid_node_features_export_render().
+ * Implements hook_features_pipe_COMPONENT_alter().
  */
-function taxonomy_uuid_node_features_export_alter(&$export, &$pipe, $node) {
-  if (!empty($node->taxonomy)) {
-    foreach ($node->taxonomy as $term) {
-      $vocabulary = taxonomy_vocabulary_load($term->vid);
-      $voc_uuid = $vocabulary->machine_name;
-      $pipe['uuid_vocabulary'][$voc_uuid] = $voc_uuid;
-
-      $term_uuid = $term->uuid;
-      $pipe['uuid_term'][$term_uuid] = $term_uuid;
+function taxonomy_features_pipe_field_instance_alter(&$pipe, $data, &$export) {
+  module_load_include('inc', 'features', 'features.export');
+  $map = features_get_default_map('uuid_term', 'uuid');
+
+  foreach ($data as $identifier) {
+    if ($instance = features_field_instance_load($identifier)) {
+      $field_info = field_info_field($instance['field_name']);
+
+      // Only act on taxonomy term reference fields with defaults.
+      if ($field_info['type'] == 'taxonomy_term_reference' && !empty($instance['default_value'])) {
+        foreach ($instance['default_value'] as $value) {
+          $term = taxonomy_term_load($value['tid']);
+
+          if (!empty($term->uuid)) {
+            // If this uuid_term is already provided by another module,
+            // remove the uuid_term and add the other module as a dependency.
+            if (isset($map[$term->uuid]) && $map[$term->uuid] != $export['module_name']) {
+              if (isset($export['features']['uuid_term'][$term->uuid])) {
+                unset($export['features']['uuid_term'][$term->uuid]);
+              }
+              $module = $map[$term->uuid];
+              $export['dependencies'][$module] = $module;
+            }
+            // Otherwise, add the uuid_term to the export and delegate it
+            // to the uuid_term export handlers.
+            else {
+              $export['features']['uuid_term'][$term->uuid] = $term->uuid;
+              $pipe['uuid_term'][] = $term->uuid;
+            }
+          }
+        }
+      }
     }
   }
 }
 
 /**
- * Implements hook_uuid_node_features_export_render_alter().
+ * Implements hook_field_instance_features_export_render_alter().
  */
-function taxonomy_uuid_node_features_export_render_alter(&$export, &$node, $module) {
-  if (!empty($node->taxonomy)) {
-    $export->uuid_term = array();
-    foreach ($node->taxonomy as $term) {
-      $export->uuid_term[] = $term->uuid;
+function taxonomy_field_instance_features_export_render_alter(&$instance) {
+  $field_info = field_info_field($instance['field_name']);
+
+  // Only act on taxonomy term reference fields with defaults.
+  if ($field_info['type'] == 'taxonomy_term_reference' && !empty($instance['default_value'])) {
+    $default_values = array();
+
+    foreach ($instance['default_value'] as $value) {
+      $term = taxonomy_term_load($value['tid']);
+
+      if (!empty($term->uuid)) {
+        $default_values[] = array('uuid' => $term->uuid);
+      }
+      else {
+        watchdog('uuid_features', 'The uuid of term %tid could not be found.', array('%tid' => $term->tid));
+      }
     }
+
+    $instance['default_value'] = $default_values;
   }
 }
 
 /**
- * Implements hook_uuid_node_features_rebuild_alter().
- * Build a taxonomy array suitable for node_save() from the uuid_term array.
+ * Implements hook_field_instance_features_rebuild_alter().
  */
-function taxonomy_uuid_node_features_rebuild_alter(&$node, $module) {
-  if (!empty($node->uuid_term)) {
-    $node->taxonomy = array();
-    foreach ($node->uuid_term as $uuid) {
-      $tid = uuid_taxonomy_term_find($uuid);
-      if (empty($tid)) {
-        // If no tid was found, then the term doesn't exist, and most likely
-        // the uuid_term rebuild needs to run first.
-        // TODO: figure out how to weight feature components.
-        uuid_term_features_rebuild($module);
-
-        // Now try again.
-        $tid = uuid_taxonomy_term_find($uuid);
-        if (empty($tid)) {
+function taxonomy_field_instance_features_rebuild_alter(&$instance) {
+  $field_info = field_info_field($instance['field_name']);
+
+  // Only act on taxonomy term reference fields with defaults.
+  if ($field_info['type'] == 'taxonomy_term_reference' && !empty($instance['default_value'])) {
+    foreach ($instance['default_value'] as $key => $value) {
+      if (!empty($value['uuid'])) {
+        $terms = entity_uuid_load('taxonomy_term', array($value['uuid']));
+        $tid = key($terms);
+
+        if (!empty($tid)) {
+          $instance['default_value'][$key]['tid'] = $tid;
+        }
+        else {
           watchdog('uuid_features', 'The term specified by %uuid could not be found.', array('%uuid' => $uuid));
-          continue;
         }
       }
-
-      $node->taxonomy[] = $tid;
     }
   }
 }
diff --git a/includes/uuid_bean.features.inc b/includes/uuid_bean.features.inc
index 475e453..fbfa247 100644
--- a/includes/uuid_bean.features.inc
+++ b/includes/uuid_bean.features.inc
@@ -37,8 +37,6 @@ function uuid_bean_features_export($data, &$export, $module_name = '') {
   $export['dependencies']['bean_uuid'] = 'bean_uuid';
   $export['dependencies']['uuid_features'] = 'uuid_features';
 
-  uuid_features_load_module_includes();
-
   $bids = entity_get_id_by_uuid('bean', $data);
   foreach ($bids as $uuid => $bid) {
     // Load the bean matching the $bid.
@@ -85,8 +83,6 @@ function uuid_bean_features_export($data, &$export, $module_name = '') {
 function uuid_bean_features_export_render($module, $data) {
   $translatables = $code = array();
 
-  uuid_features_load_module_includes();
-
   $code[] = '  $beans = array();';
   $code[] = '';
   foreach ($data as $uuid) {
diff --git a/includes/uuid_node.features.inc b/includes/uuid_node.features.inc
index 54974d2..e977112 100755
--- a/includes/uuid_node.features.inc
+++ b/includes/uuid_node.features.inc
@@ -37,8 +37,6 @@ function uuid_node_features_export($data, &$export, $module_name = '') {
 
   $export['dependencies']['uuid_features'] = 'uuid_features';
 
-  uuid_features_load_module_includes();
-
   $nids = entity_get_id_by_uuid('node', $data);
   foreach ($nids as $uuid => $nid) {
     // Load the existing node, with a fresh cache.
@@ -66,8 +64,6 @@ function uuid_node_features_export($data, &$export, $module_name = '') {
 function uuid_node_features_export_render($module, $data) {
   $translatables = $code = array();
 
-  uuid_features_load_module_includes();
-
   $code[] = '  $nodes = array();';
   $code[] = '';
   $nids = entity_get_id_by_uuid('node', $data);
diff --git a/uuid_features.module b/uuid_features.module
index c42f4ef..c4cb1f7 100644
--- a/uuid_features.module
+++ b/uuid_features.module
@@ -90,6 +90,15 @@ function uuid_features_load_module_includes() {
 }
 
 /**
+ * Implements hook_init().
+ *
+ * Make sure the polyfills are always loaded.
+ */
+function uuid_features_init() {
+  uuid_features_load_module_includes();
+}
+
+/**
  * Implements hook_features_pipe_COMPONENT_alter().
  *
  * When exporting a vocabulary, include its terms.
-- 
1.7.9.4

