From f2534aaf88d0ae14cc16e7d3a1d01fb0a8ad47fc Mon Sep 17 00:00:00 2001
From: Pieter Frenssen <pieter@frenssen.be>
Date: Wed, 25 Jul 2012 21:54:39 +0200
Subject: [PATCH] Issue #1700608 by pfrenssen: Redirect to the active space
 when viewing a node.

---
 spaces_taxonomy/plugins/space_taxonomy.inc | 20 ++-------------
 spaces_taxonomy/spaces_taxonomy.module     | 39 ++++++++++++++++++++++++++++++
 2 files changed, 41 insertions(+), 18 deletions(-)

diff --git a/spaces_taxonomy/plugins/space_taxonomy.inc b/spaces_taxonomy/plugins/space_taxonomy.inc
index e103a4d..7c6ec73 100644
--- a/spaces_taxonomy/plugins/space_taxonomy.inc
+++ b/spaces_taxonomy/plugins/space_taxonomy.inc
@@ -44,24 +44,8 @@ class space_taxonomy extends space_type_purl {
         }
         break;
       case 'node':
-        $node = $object;
-        $vocab = variable_get('spaces_taxonomy_machine_name', 0);
-        $fields = field_info_fields();
-        foreach ($fields as $name => $field) {
-          if ($field['type'] == 'taxonomy_term_reference' && $vocab == $field['settings']['allowed_values'][0]['vocabulary']) {
-            $term = NULL;
-            if (isset($node->{$name}['und'][0]['taxonomy_term'])) {
-              $term = $node->{$name}['und'][0]['taxonomy_term'];
-            }
-            elseif (isset($node->{$name}['und'][0]['tid'])) {
-              $term = taxonomy_term_load($node->{$name}['und'][0]['tid']);
-            }
-
-            if ($term) {
-              $this->router_term($term);
-              return;
-            }
-          }
+        if ($term = spaces_taxonomy_get_term($object)) {
+          $this->router_term($term);
         }
         break;
     }
diff --git a/spaces_taxonomy/spaces_taxonomy.module b/spaces_taxonomy/spaces_taxonomy.module
index 9769f8f..d73ab68 100644
--- a/spaces_taxonomy/spaces_taxonomy.module
+++ b/spaces_taxonomy/spaces_taxonomy.module
@@ -71,6 +71,45 @@ function spaces_taxonomy_spaces_registry() {
 }
 
 /**
+ * Implements hook_spaces_get_space_from_object().
+ */
+function spaces_taxonomy_spaces_get_space_from_object($type, $object) {
+  if ($type == 'node') {
+    if ($term = spaces_taxonomy_get_term($object)) {
+      return spaces_load('taxonomy', $term->tid);
+    }
+  }
+}
+
+/**
+ * Searches the given node for a term reference that represents a space.
+ *
+ * @param object $node
+ *   A node object.
+ *
+ * @return object|FALSE
+ *   The term object that is referenced in the node, or FALSE if it was not
+ *   found.
+ */
+function spaces_taxonomy_get_term($node) {
+  $term = FALSE;
+  $vocab = variable_get('spaces_taxonomy_machine_name', 0);
+  $fields = field_info_fields();
+  foreach ($fields as $name => $field) {
+    if ($field['type'] == 'taxonomy_term_reference' && $vocab == $field['settings']['allowed_values'][0]['vocabulary']) {
+      if (isset($node->{$name}['und'][0]['taxonomy_term'])) {
+        $term = $node->{$name}['und'][0]['taxonomy_term'];
+      }
+      elseif (isset($node->{$name}['und'][0]['tid'])) {
+        $term = taxonomy_term_load($node->{$name}['und'][0]['tid']);
+      }
+      break;
+    }
+  }
+  return $term;
+}
+
+/**
  * Implements hook_menu().
  */
 function spaces_taxonomy_menu() {
-- 
1.7.11.2

