Taxonomy Vocabulary Relate was very helpful module in Drupal 6.x which allows us to select related terms in taxonomy from other vocaulary. Default taxonomy module in Drupal 6.x only allow us to select related terms from the same vocabulary. This post is going to help lots of developers who are planning to migrate their Drupal 6 website to Drupal 7 or who have migrated their projects and are awaiting solution to this issue.

After doing analysis of migration pre-requisites this was kind of a show stoper, there is no 7.x version available and the perfect reason is that in Drupal 7 taxonomies are now part of entities and are fieldable, so one who is familier with the entities can easily find similar solution by creating a term reference field for the vocabulary for which the term needs a reference.

I have two Vocabularies, "Organization" and "Service Catalog" with vocabulary name as "vocabulary_6" and "vocabulary_10" respectively. Now the scenario here is that we want to relate "Organization" with "Service Catalog", means more than one "Service Catalog" term can have one "Organization" term, so its many to one relationship. Below are the steps on how to get this module and data migrated for the above mentioned scenario.

  • Edit the Serivce Catalog Vocabulary, go to "Manage Fields" tab and add a field of type "Term reference" having widget "Select list" which will have terms from "Organization" Vocabulary.
  • Edit the terms in "Service Catalog" Vocabulary and you will see that field with terms listing from "Organization" vocabulary, select your related term and save. Do this for all the terms in "Service Catalog" who can have relation with terms in "Organization" vocabulary.

Above two steps are workable if you have limited terms that needs migration, but if you have lots of terms and is not possible to convert all terms and associate related terms one by one, you can use the alogrithm below and write small PHP code that will move the related term data to relevant tables.

In my case the field that I created in "Service Catalog" to relate "Organization" terms is called "taxonomy_vocabulary_6", In Drupal 7 the relavent mysql tables for this field will be:

  • field_data_taxonomy_vocabulary_6
  • field_revision_taxonomy_vocabulary_6

Now you have to move data from old table "term_relation" which will be called "taxonomy_term_relation" after Drupal 7 is migrated. Analyze this table and you will find two columns "tid1" and "tid2" where "tid1" is holding Term IDs from "Organization" Vocabulary and "tid2" is holding Term IDs from "Service Catalog" Vocabulary. Just make sure as validation test that the term in "tid1" that you are going to move belongs to "Organization" Vocabulary.

<?php

  $result = db_query('SELECT tid1, tid2 FROM {taxonomy_term_relation}');

  foreach ($result as $record) {
    db_insert('field_data_taxonomy_vocabulary_6')
      ->fields(array(
        'entity_type' -> 'taxonomy_term',
        'bundle' -> 'vocabulary_10', // Machine name of Service Catalog Vocabulary
        'deleted' -> 0,
        'entity_id' -> $record->'tid2', // tid of Service Catalog term
        'revision_id' -> $record->'tid2', // tid of Service Catalog term
        'language' -> 'und',
        'delta' -> 0',
        'taxonomy_vocabulary_6_tid' -> $record->'tid1, // tid of Organization term
      ))
      ->execute();

    db_insert('field_revision_taxonomy_vocabulary_6')
      ->fields(array(
        'entity_type' -> 'taxonomy_term',
        'bundle' -> 'vocabulary_10', // Machine name of Service Catalog Vocabulary
        'deleted' -> 0,
        'entity_id' -> $record->'tid2', // tid of Service Catalog term
        'revision_id' -> $record->'tid2', // tid of Service Catalog term
        'language' -> 'und',
        'delta' -> 0',
        'taxonomy_vocabulary_6_tid' -> $record->'tid1, // tid of Organization term
      ))
      ->execute();  
  }

?>