Using the example/suggestion @Molot posted, I created a batch process and packaged it as a module. Thought I would share.
First, create your taxonomy terms and add your entity reference field to your content type, then create and enable your batch module. Be sure to update the **FIELDS**
below.
/**
* Implements hook_menu().
*/
// Lets add a menu link to initiate the batch process
function convert_field_to_taxonomy_menu() {
$items = array();
$items['admin/batch'] = array(
'title' => 'Convert to Tax Batch',
'description' => 'Run batch operations.',
'page callback' => 'drupal_get_form',
'page arguments' => array('convert_field_to_taxonomy_form'),
'access arguments' => array('administer site configuration'),
'type' => MENU_NORMAL_ITEM,
);
return $items;
}
// And a button to click
function convert_field_to_taxonomy_form() {
$form = array();
$form['submit'] = array('#type' => 'submit', '#value' => t('Click here to Start'));
return $form;
}
function convert_field_to_taxonomy_form_submit($form, $form_state) {
batch_set(convert_field_to_taxonomy_setup_batch());
}
function convert_field_to_taxonomy_setup_batch() {
drupal_set_message('Updating Nodes');
// load all the nodes from content type
$nodes = node_load_multiple(array(), array('type' => "**CONTENT_TYPE**"));
$node_count = count($nodes);
// build the list of operation functions and function arguments
foreach($nodes as $nid => $node) {
// $operations[] = array(<function name>, <array of arguments to pass to function>);
$operations[] = array('convert_field_to_taxonomy_method', array($node));
}
//put all that information into our batch array
$batch = array(
'operations' => $operations,
'title' => t('Convert to Tax Batch'),
'init_message' => t('Initializing...'),
'error_message' => t('An error occurred'),
'progress_message' => t('Operation @current out of @total.'),
'finished' => 'convert_field_to_taxonomy_finished_method'
);
return $batch;
}
function convert_field_to_taxonomy_method($node, &$context) {
$context['results'][] = $node->nid.
' : '.check_plain($node->title);
// Optional message displayed under the progressbar.
$context['message'] = t('Processing snapshot "@title"', array('@title' => $node->title));
$vid = 38; // vocab ID
$edit = array('vid' => $vid, 'name' => $node->**SOURCE_FIELD**['und'][0]['value']);
$terms = taxonomy_get_term_by_name($edit['name']);
if (!empty($terms)) {
// term already exists
$first_item = array_shift($terms);
$tid = $first_item->tid;
} else {
// add term and get the tid
$status = taxonomy_term_save($edit);
$tid = $edit['tid'];
}
$node->**TARGET_FIELD**['und'][0]['target_id'] = $tid;
node_save($node);
$path = drupal_lookup_path("alias", "node/".$node->nid);
drupal_set_message("<a href='/$path'>".$node->title.
"</a> updated.");
}
function convert_field_to_taxonomy_finished_method($success, $results, $operations) {
if ($success) {
// Here we could do something meaningful with the results.
// We just display the number of data we processed...
drupal_set_message(t('@count terms processed.', array('@count' => count($results))));
} else {
// An error occurred.
// $operations contains the operations that remained unprocessed.
$error_operation = reset($operations);
drupal_set_message(t('An error occurred while processing @operation with arguments : @args', array('@operation' => $error_operation[0], '@args' => print_r($error_operation[0], TRUE))));
}
}