Solved

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

Posted on 2011-03-10
6
877 Views
Last Modified: 2012-05-11
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.
0
Comment
Question by:xamian
  • 4
  • 2
6 Comments
 
LVL 16

Accepted Solution

by:
HagayMandel earned 500 total points
ID: 35115543
You say:

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
If you don't have writing permission, you'll have to bypass Drupal's taxonomy management, and do things directly on the DB:
Drupal's taxonomy is consisting of 5 tables in the DB:
term_data
term_hierarchy
term_node
term_relation
term_synonym

The first table holds the taxonomy terms, the second, the hierarchy (parent-child), and the third holds the list of terms' related nodes.
I don't have experience with the other 2 tables.
You can use a php script to populate these tables from any file.

0
 
LVL 1

Author Comment

by:xamian
ID: 35116200
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?
0
 
LVL 16

Expert Comment

by:HagayMandel
ID: 35116761
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.

0
Backup Solution for AWS

Read about how CloudBerry Backup fully integrates your backups with Amazon S3 and Amazon Glacier to provide military-grade encryption and dramatically cut storage costs on any platform.

 
LVL 16

Expert Comment

by:HagayMandel
ID: 35119780
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

0
 
LVL 1

Author Comment

by:xamian
ID: 35128995
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

0
 
LVL 16

Expert Comment

by:HagayMandel
ID: 35129360
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.
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article is for those that are having major problems with users upload files such as pictures to their profile. The solution is simple and has to do with correcting the directory paths. With some experimenting and testing i got it fixed. Noteā€¦
In this blog, I will share you some basic tips for content marketing and to rank your website on Google.
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

740 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question