From 91ccdda421835fe3091c9204d912a6dbd1a5d57a Mon Sep 17 00:00:00 2001
From: Angie Byron <webchick@24967.no-reply.drupal.org>
Date: Thu, 11 Feb 2010 03:29:22 +0000
Subject: [PATCH] #628244 by catch, bangpound, and yched: Remove magic taxonomy
 field creation in favour of consistency.

---
 modules/forum/forum.install        | 32 ++++++++++++++++++++----------
 modules/taxonomy/taxonomy.module   | 26 ------------------------
 modules/taxonomy/taxonomy.test     | 30 ++++++++++++++++++++++++++++
 profiles/standard/standard.install | 17 ++++++++++++++++
 4 files changed, 68 insertions(+), 37 deletions(-)

diff --git a/modules/forum/forum.install b/modules/forum/forum.install
index 979acacc5cdf..5fb0cc9e8ef6 100644
--- a/modules/forum/forum.install
+++ b/modules/forum/forum.install
@@ -21,17 +21,9 @@ function forum_install() {
 }
 
 function forum_enable() {
-  // Get info on the field "taxonomy_forums".
-  $field_check = field_info_field("taxonomy_forums");
-  if ($vocabulary = taxonomy_vocabulary_load(variable_get('forum_nav_vocabulary', 0))) {
-    // Save the vocabulary to create the default field instance.
-    taxonomy_vocabulary_save($vocabulary);
-  }
-  // Create fields if info on "taxonomy_forums" field turns out empty.
-  elseif (empty($field_check)) {
-    // Create the forum vocabulary if it does not exist. Assign the vocabulary
-    // a low weight so it will appear first in forum topic create and edit
-    // forms.
+  // Create the forum vocabulary if it does not exist.
+  $vocabulary = taxonomy_vocabulary_load(variable_get('forum_nav_vocabulary', 0));
+  if (!$vocabulary) {
     $edit = array(
       'name' => t('Forums'),
       'machine_name' => 'forums',
@@ -42,6 +34,24 @@ function forum_enable() {
     );
     $vocabulary = (object) $edit;
     taxonomy_vocabulary_save($vocabulary);
+    variable_set('forum_nav_vocabulary', $vocabulary->vid);
+  }
+
+  // Create the 'taxonomy_forums' field if it doesn't already exist.
+  if (!field_info_field('taxonomy_forums')) {
+    $field = array(
+      'field_name' => 'taxonomy_' . $vocabulary->machine_name,
+      'type' => 'taxonomy_term_reference',
+      'settings' => array(
+        'allowed_values' => array(
+          array(
+            'vid' => $vocabulary->vid,
+            'parent' => 0,
+          ),
+        ),
+      ),
+    );
+    field_create_field($field);
 
     $instance = array(
       'field_name' => 'taxonomy_' . $vocabulary->machine_name,
diff --git a/modules/taxonomy/taxonomy.module b/modules/taxonomy/taxonomy.module
index 13578be04784..dcfe129cbb9e 100644
--- a/modules/taxonomy/taxonomy.module
+++ b/modules/taxonomy/taxonomy.module
@@ -371,7 +371,6 @@ function taxonomy_vocabulary_save($vocabulary) {
   elseif (empty($vocabulary->vid)) {
     $status = drupal_write_record('taxonomy_vocabulary', $vocabulary);
     field_attach_create_bundle('taxonomy_term', $vocabulary->machine_name);
-    taxonomy_vocabulary_create_field($vocabulary);
     module_invoke_all('taxonomy_vocabulary_insert', $vocabulary);
     entity_invoke('insert', 'taxonomy_vocabulary', $vocabulary);
   }
@@ -451,31 +450,6 @@ function taxonomy_check_vocabulary_hierarchy($vocabulary, $changed_term) {
   return $hierarchy;
 }
 
-/**
- * Create a default field when a vocabulary is created.
- *
- * @param $vocabulary
- *   A taxonomy vocabulary object.
- */
-function taxonomy_vocabulary_create_field($vocabulary) {
-  $field = array(
-    'field_name' => 'taxonomy_' . $vocabulary->machine_name,
-    'type' => 'taxonomy_term_reference',
-    // Set cardinality to unlimited so that select
-    // and autocomplete widgets behave as normal.
-    'cardinality' => FIELD_CARDINALITY_UNLIMITED,
-    'settings' => array(
-      'allowed_values' => array(
-        array(
-          'vid' => $vocabulary->vid,
-          'parent' => 0,
-        ),
-      ),
-    ),
-  );
-  field_create_field($field);
-}
-
 /**
  * Save a term object to the database.
  *
diff --git a/modules/taxonomy/taxonomy.test b/modules/taxonomy/taxonomy.test
index 7d5a0ef93d60..eca965fc4d21 100644
--- a/modules/taxonomy/taxonomy.test
+++ b/modules/taxonomy/taxonomy.test
@@ -324,6 +324,21 @@ class TaxonomyTermTestCase extends TaxonomyWebTestCase {
     $this->drupalLogin($this->admin_user);
     $this->vocabulary = $this->createVocabulary();
 
+    $field = array(
+      'field_name' => 'taxonomy_' . $this->vocabulary->machine_name,
+      'type' => 'taxonomy_term_reference',
+      'cardinality' => FIELD_CARDINALITY_UNLIMITED,
+      'settings' => array(
+        'allowed_values' => array(
+          array(
+            'vid' => $this->vocabulary->vid,
+            'parent' => 0,
+          ),
+        ),
+      ),
+    );
+    field_create_field($field);
+
     $this->instance = array(
       'field_name' => 'taxonomy_' . $this->vocabulary->machine_name,
       'bundle' => 'article',
@@ -855,6 +870,21 @@ class TaxonomyTokenReplaceTestCase extends TaxonomyWebTestCase {
     $this->vocabulary = $this->createVocabulary();
     $this->langcode = LANGUAGE_NONE;
 
+    $field = array(
+      'field_name' => 'taxonomy_' . $this->vocabulary->machine_name,
+      'type' => 'taxonomy_term_reference',
+      'cardinality' => FIELD_CARDINALITY_UNLIMITED,
+      'settings' => array(
+        'allowed_values' => array(
+          array(
+            'vid' => $this->vocabulary->vid,
+            'parent' => 0,
+          ),
+        ),
+      ),
+    );
+    field_create_field($field);
+
     $this->instance = array(
       'field_name' => 'taxonomy_' . $this->vocabulary->machine_name,
       'bundle' => 'article',
diff --git a/profiles/standard/standard.install b/profiles/standard/standard.install
index fd02f282c0cc..450347bb2004 100644
--- a/profiles/standard/standard.install
+++ b/profiles/standard/standard.install
@@ -304,6 +304,23 @@ function standard_install() {
 
   );
   taxonomy_vocabulary_save($vocabulary);
+
+  $field = array(
+    'field_name' => 'taxonomy_' . $vocabulary->machine_name,
+    'type' => 'taxonomy_term_reference',
+    // Set cardinality to unlimited for tagging.
+    'cardinality' => FIELD_CARDINALITY_UNLIMITED,
+    'settings' => array(
+      'allowed_values' => array(
+        array(
+          'vid' => $vocabulary->vid,
+          'parent' => 0,
+        ),
+      ),
+    ),
+  );
+  field_create_field($field);
+
   $instance = array(
     'field_name' => 'taxonomy_' . $vocabulary->machine_name,
     'object_type' => 'node',
-- 
GitLab