From 96c9a6f87c701fded505e4fb3bec34321e812e89 Mon Sep 17 00:00:00 2001
From: James Sansbury <james.sansbury@lullabot.com>
Date: Thu, 31 May 2012 09:41:42 -0400
Subject: [PATCH] Issue #1404586 by mrfelton, chriz001, q0rban: Return the
 taxonomy space for entities.

---
 spaces_taxonomy/spaces_taxonomy.module |   23 +++++++++++++++++++++++
 1 file changed, 23 insertions(+)

diff --git a/spaces_taxonomy/spaces_taxonomy.module b/spaces_taxonomy/spaces_taxonomy.module
index 9769f8f..cb910be 100644
--- a/spaces_taxonomy/spaces_taxonomy.module
+++ b/spaces_taxonomy/spaces_taxonomy.module
@@ -263,3 +263,26 @@ function spaces_taxonomy_settings($form, &$form_state) {
   $form = system_settings_form($form);
   return $form;
 }
+
+/**
+ * Implements hook_spaces_get_space_from_object().
+ */
+function spaces_taxonomy_spaces_get_space_from_object($type, $object) {
+  if ($vocab_name = variable_get('spaces_taxonomy_machine_name')) {
+    $fields = field_info_fields();
+    foreach ($fields as $field_name => $field) {
+      // If this is not a taxonomy reference field, or allowed values is empty,
+      // or this field doesn't exist on this object, just move along.
+      // TODO: Account for generic entity reference fields.
+      if ($field['type'] != 'taxonomy_term_reference'
+        || empty($field['settings']['allowed_values'][0]['vocabulary'])
+        || empty($object->$field_name)) {
+          continue;
+      }
+      if ($vocab_name == $field['settings']['allowed_values'][0]['vocabulary']) {
+        $term = $object->$field_name;
+        return spaces_load('taxonomy', $term[LANGUAGE_NONE][0]['tid']);
+      }
+    }
+  }
+}
\ No newline at end of file
-- 
1.7.10

