From a039bfa81c0d508458d480bd6302fefd51d7d00d Mon Sep 17 00:00:00 2001
From: Alex Barth <alex_b@53995.no-reply.drupal.org>
Date: Tue, 7 Sep 2010 23:09:46 +0000
Subject: [PATCH] #904804 alex_b: Support exportable vocabularies.

---
 CHANGELOG.txt        |  1 +
 mappers/taxonomy.inc | 55 ++++++++++++++++++++++++++++++++++++--------
 2 files changed, 46 insertions(+), 10 deletions(-)

diff --git a/CHANGELOG.txt b/CHANGELOG.txt
index 8001b181..88588b62 100644
--- a/CHANGELOG.txt
+++ b/CHANGELOG.txt
@@ -3,6 +3,7 @@
 Feeds 6.x 1.0 XXXXXXXXXXXXXXXXXX
 --------------------------------
 
+- #904804 alex_b: Support exportable vocabularies.
 - #836876 rsoden, Will White, alex_b: Add simple georss support to Common
   Syndication Parser.
 - #889196 David Goode: Support for non-numeric vocabulary IDs for feature-based
diff --git a/mappers/taxonomy.inc b/mappers/taxonomy.inc
index 5d65aa60..70c96f66 100644
--- a/mappers/taxonomy.inc
+++ b/mappers/taxonomy.inc
@@ -24,11 +24,7 @@ class FeedsTermElement extends FeedsElement {
 function taxonomy_feeds_parser_sources_alter(&$sources, $content_type) {
   if (!empty($content_type)) {
     foreach (taxonomy_get_vocabularies($content_type) as $vocabulary) {
-      $id = $vocabulary->vid;
-      if (strpos($vocabulary->module, 'features_') === 0) {
-        $id = $vocabulary->module;
-      }
-      $sources['parent:taxonomy:'. $id] = array(
+      $sources['parent:taxonomy:'. taxonomy_vocabulary_id($vocabulary)] = array(
         'name' => t('Feed node: Taxonomy: @vocabulary', array('@vocabulary' => $vocabulary->name)),
         'description' => t('Taxonomy terms from feed node in given vocabulary.'),
         'callback' => 'taxonomy_feeds_get_source',
@@ -43,10 +39,10 @@ function taxonomy_feeds_parser_sources_alter(&$sources, $content_type) {
 function taxonomy_feeds_get_source(FeedsImportBatch $batch, $key) {
   if ($node = $batch->feedNode()) {
     $terms = taxonomy_node_get_terms($node);
-    $vid = (int) str_replace('parent:taxonomy:', '', $key);
+    $vocabulary = taxonomy_get_vocabulary(str_replace('parent:taxonomy:', '', $key));
     $result = array();
     foreach ($terms as $tid => $term) {
-      if ($term->vid == $vid) {
+      if ($term->vid == $vocabulary->vid) {
         $result[] = new FeedsTermElement($term);
       }
     }
@@ -63,7 +59,7 @@ function taxonomy_feeds_node_processor_targets_alter(&$targets, $content_type) {
   foreach (taxonomy_get_vocabularies($content_type) as $vocabulary) {
     $description = t('The !name vocabulary of the node. If this is a "Tags" vocabulary, any new terms will be created on import. Otherwise only existing terms will be used. If this is not a "Tags" vocabulary and not a "Multiple select" vocabulary, only the first available term will be created. See !settings.', array('!name' => $vocabulary->name, '!settings' => l(t('vocabulary settings'), 'admin/content/taxonomy/edit/vocabulary/'. $vocabulary->vid, array('query' => 'destination='. $_GET['q']))));
 
-    $targets['taxonomy:'. $vocabulary->vid] = array(
+    $targets['taxonomy:'. taxonomy_vocabulary_id($vocabulary)] = array(
       'name' => "Taxonomy: ". $vocabulary->name,
       'callback' => 'taxonomy_feeds_set_target',
       'description' => $description,
@@ -96,8 +92,7 @@ function taxonomy_feeds_set_target(&$node, $key, $terms) {
   }
 
   // Load target vocabulary to check, if it has the "tags" flag.
-  $vid = (int) str_replace('taxonomy:', '', $key);
-  $vocabulary = taxonomy_vocabulary_load($vid);
+  $vocabulary = taxonomy_get_vocabulary(str_replace('taxonomy:', '', $key));
 
   // Cast a given single string to an array so we can use it.
   if (!is_array($terms)) {
@@ -155,3 +150,43 @@ function taxonomy_get_term_by_name_vid($name, $vid) {
   }
   return $result;
 }
+
+/**
+ * Look up a vocabulary by vid or module name.
+ *
+ * @param $id
+ *   A module name or a numeric vocabulary id.
+ *
+ * @return
+ *   An object of type stdClass that represents a vocabulary.
+ */
+function taxonomy_get_vocabulary($id) {
+  static $vocabularies;
+  if (!isset($vocabularies[$id])) {
+    foreach (taxonomy_get_vocabularies() as $vocabulary) {
+      if ($vocabulary->vid == $id) {
+        $vocabularies[$id] = $vocabulary;
+        break;
+      }
+      elseif ($vocabulary->module == $id) {
+        $vocabularies[$id] = $vocabulary;
+        break;
+      }
+    }
+  }
+  return $vocabularies[$id];
+}
+
+/**
+ * Return the vocabulary identifier, the vocabulary's vid or module.
+ *
+ * @return
+ *   Vocabulary's module name if it is a features vocabulary (= exportable),
+ *   vocabulary's vid otherwise.
+ */
+function taxonomy_vocabulary_id($vocabulary) {
+  if (strpos($vocabulary->module, 'features_') === 0) {
+    return $vocabulary->module;
+  }
+  return $vocabulary->vid;
+}
-- 
GitLab