Link to home
Start Free TrialLog in
Avatar of xamian
xamian

asked on

How can I add taxonomy terms programatically into Drupal via an CSV file?

Let me start off by saying I'm a total noob when it comes to PHP and Drupal.  I'm being tasked with the migration of legacy content off a proprietary CMS and into Drupal 6, but the powers that be do not want to install any of the helper modules that exist for migrating content into drupal (e.g. Migrate module, Features module ,Node_Import module, etc..).  I suspect that they're trying to keep the Drupal system as lean as possible.

We have a three tier development environment where the drupal installation is moved from development to staging and finally to production, I only have read access to the database so it's not like and can just go and manipulate the drupal file system or database directly.  According to my colleagues I should perform the migrations programtically by using a combination of CSV files and HOOK_UPDATE_N functions but they didn't provide me with any further instructions.  As I understood it, once the hook_updates are added to the ".install" file, they can run the "update.php" file and everything will be added precisely the same in all of the three development environments.
My project involves:
- adding the taxonomy terms programatically
- adding the new content type programatically
- migrating the legacy content into the new content type programatically

For the moment I would be very happy if I could just add the taxonomy terms programatically.  I would think that once I do that I can extrapolate that experience to accomplish the other two tasks.  If anyone can point me to an example or provide suggestions it would be greatly appreciated.
Thanks in advance.
ASKER CERTIFIED SOLUTION
Avatar of Hagay Mandel
Hagay Mandel
Flag of Israel image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of xamian
xamian

ASKER

Since I'm still pretty new to PHP sometimes I'll get confused between what's a normal php function and other functions that are drupal specific.  Should I be using HOOK_UPDATE_N like my colleagues suggest?  Don't I need to used druapls UPDATE_SQL functions, or can I just use normal PHP?
Use Drupal's db_query, for example:

$sql = 'INSERT INTO `term_data` (`tid`, `vid`, `name`, `description` , `weight` ) VALUES (\'A\', \'B\', \'C\', \'D\' , \'E\');';
$results = d($sql);

Open in new window

where A,B,C,D,E are the values from your file.

Sorry:

$sql = 'INSERT INTO `term_data` (`tid`, `vid`, `name`, `description` , `weight` ) VALUES (\'A\', \'B\', \'C\', \'D\' , \'E\');';
$results = db_query($sql);

Open in new window

Avatar of xamian

ASKER

OK, a few more questions. I'm going to have to loop through the CSV file to insert the taxonomies. Can I use standard PHP syntax to read and loop through the CSV file, or do I have to use some special Drupal function to do that?  And where should I put the CSV file so it can be read once this is moved to the other development environments?
Also, can I wrap this all in a HOOK_UPDATE function?  My colleagues told me I'm supposed to do something like this: (see code sample)
Does this make any sense?
/**
 * Update to add indexes for all join, where and order by statements
 */
function MyCustomModule_update_6003() {
  $ret = array();
  
  db_change_field($ret, 'content_field_location', 'field_location_value', 'field_location_value', array('type' => 'int', 'size' => 'medium', 'not null' => FALSE, 'default' => NULL));
  
  return $ret;
}

Open in new window

You can use standard php for the file reading.
I don't think you need to use the  HOOK_UPDATE; this  hook is for updating related tables, when a node is updated. see http://api.drupal.org/api/drupal/developer--hooks--node.php/function/hook_update/6.