Migrating data in Drupal 5 to 6 using Table Wizard and Migrate (Part 4 - Product Nodes)

Product nodes

So the next step is bringing in the product nodes. As I mentioned in the company node migrate post, I have some custom text fields with taxonomy terms in them, as well as a regular taxonomy, and a node reference field (for the company that makes the product).

The custom text fields' data and the regular taxonomy terms must get moved into new Content Taxonomy fields. The node reference must have its node id number updated to get the new company node id.

Using the same hook_prepare_node function, like I did for company nodes, I will add in a new case for product nodes, and stick all the prepare code in there. Here's the code:

<?php
 
case 'product':
   
// Get product group, sub-category and feature term ids
    // from the 3 custom text fields used in the D5 site.
    // Find the new tid using a custom function and add it to
    // an array of tids
   
$tids = array();
   
$new_group_id = _get_new_tids($node->field_product_group[0]['value'], 'migrate_map_b_term_parent');
   
$node->field_product_group[0]['value'] = $new_group_id;
   
$tids[] = $new_group_id;

    $new_subcat_id = _get_new_tids($node->field_subcat[0]['value'], 'migrate_map_b_term_parent');
   
$node->field_subcat[0]['value'] = $new_subcat_id;
   
$tids[] = $new_subcat_id;

    // have to manually lookup the feature tids as there are
    // multiple values here, whereas the previous two fields
    // were single values only.
   
$result = db_query('SELECT field_feature_value FROM b_content_field_feature WHERE nid = %d', $row->nid);
    while (
$terms = db_fetch_object($result)) {
     
// add each tid to the array after getting the new value
     
$tids[] = _get_new_tids($terms->field_feature_value, 'migrate_map_b_term_parent');
    }

    foreach ($tids as $tid) {
     
// To add the terms to taxonomy uncomment this next line
      //$node->taxonomy[] = taxonomy_get_term($tid);

      // However, there is no need to add to taxonomy as the
      // product_category field is a content_taxonomy field
      // and its set to add it to taxonomy already. You will get
      // an error if you try to do both.
     
      // get the name of the term
      $term_name = _get_term_name($tid);
     
// create an array for the content_taxonomy field
     
$term = array('value' => $tid, 'view' => $term_name);
     
// assign it to the field
     
$node->field_product_category[] = $term;  
    }

    // get product other feature term ids - this is the regular
    // taxonomy field that will become a content taxonomy field
   
$pofs_arr = array();
   
// first find all tids for the "other features" vocabulary
   
$pof_result = db_query('SELECT tn.tid FROM b_term_node tn LEFT JOIN b_term_data td ON tn.tid = td.tid WHERE nid = %d AND vid = 6', $row->nid);
    while (
$pofs = db_fetch_object($pof_result)) {
     
$pofs_arr[] = _get_new_tids($pofs->tid, 'migrate_map_b_prod_other_terms');
    }
   
// as before, get the term name, create the array and assign
    // it to the content taxonomy field. It will automatically
    // get added to taxonomy as well.
   
foreach ($pofs_arr as $pof) {
     
$pof_name = _get_term_name($pof);
     
$pof_term = array('value' => $pof, 'view' => $pof_name);
     
$node->field_product_other_features[] = $pof_term;
    }

    // get new company node id for the node reference field
   
$node->field_company[0]['nid'] = _get_new_tids($node->field_company[0]['nid'], 'migrate_map_b_company_nodes');
    break;
?>

The custom functions that look up the term name and get the new term id are here:

<?php
/*
* Convert old category term ids to new tids.
*
* @param $oldid
*  Old category term.
* @param $table
*  Text string for the migrate map table to look in.
* @return
*  New tid.
*/
function _get_new_tids($oldid, $table) {
 
$result = db_result(db_query('SELECT destid FROM %s WHERE sourceid = %d', $table, $oldid));
  return
$result;
}

/**
* Lookup a term's name.
*
* @param $tid
*  The term id number.
* @return
*  The name of the term.
*/
function _get_term_name($tid) {
 
$name = db_result(db_query('SELECT name FROM {term_data} WHERE tid = %d', $tid));
  return
$name;
}
?>

To do the actual migrate, here are the steps I took:

Posted in:
magnanimous-junior