From 823c0a0a55604429874702d64a85363099224aa1 Mon Sep 17 00:00:00 2001
From: Angie Byron <webchick@24967.no-reply.drupal.org>
Date: Fri, 1 Oct 2010 01:37:13 +0000
Subject: [PATCH] #881530 by yhahn: Fix exportability of vocabularies by using
 machine names in taxonomy field's 'allowed values' setting.

---
 modules/forum/forum.install                   |  2 +-
 .../tests/upgrade/upgrade.taxonomy.test       | 10 +++---
 modules/taxonomy/taxonomy.install             |  4 +--
 modules/taxonomy/taxonomy.module              | 35 ++++++++++---------
 modules/taxonomy/taxonomy.test                |  8 ++---
 profiles/standard/standard.install            |  2 +-
 6 files changed, 33 insertions(+), 28 deletions(-)

diff --git a/modules/forum/forum.install b/modules/forum/forum.install
index 9a1ae18f691e..bc2a9b1b6b9a 100644
--- a/modules/forum/forum.install
+++ b/modules/forum/forum.install
@@ -52,7 +52,7 @@ function forum_enable() {
       'settings' => array(
         'allowed_values' => array(
           array(
-            'vid' => $vocabulary->vid,
+            'vocabulary' => $vocabulary->machine_name,
             'parent' => 0,
           ),
         ),
diff --git a/modules/simpletest/tests/upgrade/upgrade.taxonomy.test b/modules/simpletest/tests/upgrade/upgrade.taxonomy.test
index e3e3c3d63a6a..72be9ef8ae89 100644
--- a/modules/simpletest/tests/upgrade/upgrade.taxonomy.test
+++ b/modules/simpletest/tests/upgrade/upgrade.taxonomy.test
@@ -34,8 +34,8 @@ class UpgradePathTaxonomyTestCase extends UpgradePathTestCase {
         foreach ($field['settings']['allowed_values'] as $tree) {
           // Prefer valid taxonomy term reference fields for a given vocabulary
           // when they exist.
-          if (empty($instances[$tree['vid']]) || $instances[$tree['vid']] == 'taxonomyextra') {
-            $instances[$tree['vid']] = $field['field_name'];
+          if (empty($instances[$tree['vocabulary']]) || $instances[$tree['vocabulary']] == 'taxonomyextra') {
+            $instances[$tree['vocabulary']] = $field['field_name'];
           }
         }
       }
@@ -59,8 +59,10 @@ class UpgradePathTaxonomyTestCase extends UpgradePathTestCase {
 
     // Check that the node type 'page' has been associated to a taxonomy
     // reference field for each vocabulary.
-    $vocabularies = taxonomy_get_vocabularies();
-    $voc_keys = array_keys($vocabularies);
+    $voc_keys = array();
+    foreach (taxonomy_get_vocabularies() as $vocab) {
+      $voc_keys[] = $vocab->machine_name;
+    }
     $instances = $this->instanceVocabularies('node', 'page');
     $inst_keys = array_keys($instances);
     sort($voc_keys);
diff --git a/modules/taxonomy/taxonomy.install b/modules/taxonomy/taxonomy.install
index 8bd3a31db70a..bd811096150d 100644
--- a/modules/taxonomy/taxonomy.install
+++ b/modules/taxonomy/taxonomy.install
@@ -408,7 +408,7 @@ function taxonomy_update_7004() {
         'required' => $vocabulary->required ? TRUE : FALSE,
         'allowed_values' => array(
           array(
-            'vid' => $vocabulary->vid,
+            'vocabulary' => $vocabulary->machine_name,
             'parent' => 0,
           ),
         ),
@@ -466,7 +466,7 @@ function taxonomy_update_7004() {
   $allowed_values = array();
   foreach (_update_7002_taxonomy_get_vocabularies() as $vocabulary) {
     $allowed_values[] = array(
-      'vid' => $vocabulary->vid,
+      'vocabulary' => $vocabulary->machine_name,
       'parent' => 0,
     );
   }
diff --git a/modules/taxonomy/taxonomy.module b/modules/taxonomy/taxonomy.module
index 107bf7aa7e12..d73196313d36 100644
--- a/modules/taxonomy/taxonomy.module
+++ b/modules/taxonomy/taxonomy.module
@@ -1058,7 +1058,7 @@ function taxonomy_implode_tags($tags, $vid = NULL) {
  *
  * Field settings:
  * - allowed_values: a list array of one or more vocabulary trees:
- *   - vid: a vocabulary ID.
+ *   - vocabulary: a vocabulary machine name.
  *   - parent: a term ID of a term whose children are allowed. This should be
  *     '0' if all terms in a vocabulary are allowed. The allowed values do not
  *     include the parent term.
@@ -1074,7 +1074,7 @@ function taxonomy_field_info() {
       'settings' => array(
         'allowed_values' => array(
           array(
-            'vid' => '0',
+            'vocabulary' => '',
             'parent' => '0',
           ),
         ),
@@ -1148,8 +1148,8 @@ function taxonomy_field_validate($entity_type, $entity, $field, $instance, $lang
         $validate = FALSE;
         foreach ($field['settings']['allowed_values'] as $settings) {
           // If no parent is specified, check if the term is in the vocabulary.
-          if (isset($settings['vid']) && empty($settings['parent'])) {
-            if ($settings['vid'] == $terms[$item['tid']]->vid) {
+          if (isset($settings['vocabulary']) && empty($settings['parent'])) {
+            if ($settings['vocabulary'] == $terms[$item['tid']]->vocabulary_machine_name) {
               $validate = TRUE;
               break;
             }
@@ -1258,10 +1258,11 @@ function taxonomy_field_formatter_view($entity_type, $entity, $field, $instance,
 function taxonomy_allowed_values($field) {
   $options = array();
   foreach ($field['settings']['allowed_values'] as $tree) {
-    $terms = taxonomy_get_tree($tree['vid'], $tree['parent']);
-    if ($terms) {
-      foreach ($terms as $term) {
-        $options[$term->tid] = str_repeat('-', $term->depth) . $term->name;
+    if ($vocabulary = taxonomy_vocabulary_machine_name_load($tree['vocabulary'])) {
+      if ($terms = taxonomy_get_tree($vocabulary->vid, $tree['parent'])) {
+        foreach ($terms as $term) {
+          $options[$term->tid] = str_repeat('-', $term->depth) . $term->name;
+        }
       }
     }
   }
@@ -1360,9 +1361,11 @@ function taxonomy_autocomplete_validate($element, &$form_state) {
   if ($tags = $element['#value']) {
     // Collect candidate vocabularies.
     $field = $form_state['field'][$element['#field_name']][$element['#language']]['field'];
-    $vids = array();
+    $vocabularies = array();
     foreach ($field['settings']['allowed_values'] as $tree) {
-      $vids[] = $tree['vid'];
+      if ($vocabulary = taxonomy_vocabulary_machine_name_load($tree['vocabulary'])) {
+        $vocabularies[$vocabulary->vid] = $vocabulary;
+      }
     }
 
     // Translate term names into actual terms.
@@ -1370,14 +1373,14 @@ function taxonomy_autocomplete_validate($element, &$form_state) {
     foreach ($typed_terms as $typed_term) {
       // See if the term exists in the chosen vocabulary and return the tid;
       // otherwise, create a new 'autocreate' term for insert/update.
-      if ($possibilities = taxonomy_term_load_multiple(array(), array('name' => trim($typed_term), 'vid' => $vids))) {
+      if ($possibilities = taxonomy_term_load_multiple(array(), array('name' => trim($typed_term), 'vid' => array_keys($vocabularies)))) {
         $term = array_pop($possibilities);
       }
       else {
-        $vocabulary = taxonomy_vocabulary_load($vids[0]);
+        $vocabulary = reset($vocabularies);
         $term = array(
           'tid' => 'autocreate',
-          'vid' => $vids[0],
+          'vid' => $vocabulary->vid,
           'name' => $typed_term,
           'vocabulary_machine_name' => $vocabulary->machine_name,
         );
@@ -1403,17 +1406,17 @@ function taxonomy_field_settings_form($field, $instance, $has_data) {
   $vocabularies = taxonomy_get_vocabularies();
   $options = array();
   foreach ($vocabularies as $vocabulary) {
-    $options[$vocabulary->vid] = $vocabulary->name;
+    $options[$vocabulary->machine_name] = $vocabulary->name;
   }
   $form['allowed_values'] = array(
     '#tree' => TRUE,
   );
 
   foreach ($field['settings']['allowed_values'] as $delta => $tree) {
-    $form['allowed_values'][$delta]['vid'] = array(
+    $form['allowed_values'][$delta]['vocabulary'] = array(
       '#type' => 'select',
       '#title' => t('Vocabulary'),
-      '#default_value' => $tree['vid'],
+      '#default_value' => $tree['vocabulary'],
       '#options' => $options,
       '#required' => TRUE,
       '#description' => t('The vocabulary which supplies the options for this field.'),
diff --git a/modules/taxonomy/taxonomy.test b/modules/taxonomy/taxonomy.test
index 9edc6c70f33b..766e1832f160 100644
--- a/modules/taxonomy/taxonomy.test
+++ b/modules/taxonomy/taxonomy.test
@@ -379,7 +379,7 @@ class TaxonomyTermTestCase extends TaxonomyWebTestCase {
       'settings' => array(
         'allowed_values' => array(
           array(
-            'vid' => $this->vocabulary->vid,
+            'vocabulary' => $this->vocabulary->machine_name,
             'parent' => 0,
           ),
         ),
@@ -845,7 +845,7 @@ class TaxonomyTermFieldTestCase extends TaxonomyWebTestCase {
       'settings' => array(
         'allowed_values' => array(
           array(
-            'vid' => $this->vocabulary->vid,
+            'vocabulary' => $this->vocabulary->machine_name,
             'parent' => '0',
           ),
         ),
@@ -905,7 +905,7 @@ class TaxonomyTermFieldTestCase extends TaxonomyWebTestCase {
       'settings' => array(
         'allowed_values' => array(
           array(
-            'vid' => $this->vocabulary->vid,
+            'vocabulary' => $this->vocabulary->machine_name,
             'parent' => '0',
           ),
         ),
@@ -981,7 +981,7 @@ class TaxonomyTokenReplaceTestCase extends TaxonomyWebTestCase {
       'settings' => array(
         'allowed_values' => array(
           array(
-            'vid' => $this->vocabulary->vid,
+            'vocabulary' => $this->vocabulary->machine_name,
             'parent' => 0,
           ),
         ),
diff --git a/profiles/standard/standard.install b/profiles/standard/standard.install
index 19f4cc9dfe70..745c43b37426 100644
--- a/profiles/standard/standard.install
+++ b/profiles/standard/standard.install
@@ -297,7 +297,7 @@ function standard_install() {
     'settings' => array(
       'allowed_values' => array(
         array(
-          'vid' => $vocabulary->vid,
+          'vocabulary' => $vocabulary->machine_name,
           'parent' => 0,
         ),
       ),
-- 
GitLab