# HG changeset patch
# Parent f64d5baaf25f784838699a9c5869e95a422749bd
# User gpaddock
# Date 1331446095 18000

Correct DDO-1477292 by using machine names instead of vocabulary IDs.

diff --git a/modules/hs_taxonomy.install b/modules/hs_taxonomy.install
--- a/modules/hs_taxonomy.install
+++ b/modules/hs_taxonomy.install
@@ -62,3 +62,25 @@
 function hs_taxonomy_update_7301() {
   variable_set('taxonomy_override_selector', TRUE);
 }
+
+/**
+ * Convert Taxonomy vocabulary config IDs to use machine name instead of serial
+ * vocabulary ID.
+ */
+function hs_taxonomy_update_7302() {
+  require_once DRUPAL_ROOT . '/' . drupal_get_path('module', 'hierarchical_select') . '/includes/common.inc';  
+  
+  $vocabularies = taxonomy_vocabulary_get_names();
+
+  foreach ($vocabularies as $machine_name => $vocabulary) {
+    $old_config_id = "taxonomy-{$vocabulary->vid}";
+    $new_config_id = "taxonomy-{$machine_name}";
+
+    $old_config = variable_get('hs_config_' . $old_config_id, NULL);
+
+    if (!empty($old_config)) {
+      hierarchical_select_common_config_set($new_config_id, $old_config);
+      hierarchical_select_common_config_del($old_config_id);
+    }
+  }
+}
\ No newline at end of file
diff --git a/modules/hs_taxonomy.module b/modules/hs_taxonomy.module
--- a/modules/hs_taxonomy.module
+++ b/modules/hs_taxonomy.module
@@ -109,7 +109,7 @@
 function hs_taxonomy_field_settings_submit(&$form, &$form_state) {
   $field = field_info_field($form['#field_name']);
   $vocabulary = taxonomy_vocabulary_machine_name_load($field['settings']['allowed_values'][0]['vocabulary']);
-  $config = hierarchical_select_common_config_get("taxonomy-$vocabulary->vid");
+  $config = hs_taxonomy_get_config($vocabulary);
 
   if ($config['dropbox']['status'] || $config['save_lineage']) {
     $field = field_info_field($form['#field_name']);
@@ -132,7 +132,7 @@
     require_once DRUPAL_ROOT . '/' . drupal_get_path('module', 'hierarchical_select') . '/includes/common.inc';
 
     $vocabulary = taxonomy_vocabulary_machine_name_load($form['#field']['settings']['allowed_values'][0]['vocabulary']);
-    $config = hierarchical_select_common_config_get("taxonomy-$vocabulary->vid");
+    $config = hs_taxonomy_get_config($vocabulary);
 
     if ($config['dropbox']['status'] || $config['save_lineage']) {
       $form['field']['cardinality']['#disabled'] = TRUE;
@@ -255,7 +255,7 @@
     '#default_value' => $tids,
   );
 
-  hierarchical_select_common_config_apply($element, "taxonomy-$vocabulary->vid");
+  hs_taxonomy_apply_config($element, $vocabulary);
 
   // Append another #process callback that transforms #return_value to the
   // format that Field API/Taxonomy Field expects.
@@ -693,8 +693,9 @@
           if ($instance['widget']['type'] == 'taxonomy_hs') {
             $bundles_info = field_info_bundles($entity_type);
             $entity_info = entity_get_info($entity_type);
-            $vocabulary = taxonomy_vocabulary_machine_name_load($field['settings']['allowed_values'][0]['vocabulary']);
-            $config_id = "taxonomy-$vocabulary->vid";
+            $machine_name = $field['settings']['allowed_values'][0]['vocabulary'];
+            $vocabulary = taxonomy_vocabulary_machine_name_load($machine_name);
+            $config_id = "taxonomy-{$machine_name}";
             $config_info[$config_id] = array(
               'config_id'      => $config_id,
               'hierarchy type' => t('Taxonomy'),
@@ -892,6 +893,33 @@
 // Private functions.
 
 /**
+ * Load the HS taxonomy config for the provided taxonomy vocabulary.
+ * 
+ * @param   stdClass  $vocabulary
+ *                    The vocabulary for which a config will be loaded.
+ * 
+ * @return  array     The config; or, an empty array if the config could not
+ *                    be loaded. 
+ */
+function hs_taxonomy_get_config(stdClass $vocabulary) {
+  return hierarchical_select_common_config_get("taxonomy-{$vocabulary->machine_name}");
+}
+
+/**
+ * Apply the HS taxonomy config for the provided taxonomy vocabulary to the
+ * provided form element.
+ * 
+ * @param  array     $element
+ *                   The form element.
+ * 
+ * @param  stdClass  $vocabulary
+ *                   The vocabulary for which a config will be applied.
+ */
+function hs_taxonomy_apply_config(array &$element, stdClass $vocabulary) {
+  hierarchical_select_common_config_apply($element, "taxonomy-{$vocabulary->machine_name}");
+}
+
+/**
  * Drupal core's taxonomy_term_count_nodes() is buggy. See
  * http://drupal.org/node/144969#comment-843000.
  */
