Solved

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

Posted on 2011-03-10
6
862 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
NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

 
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

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Joomla - Add a new postion 30 112
cloud 2 253
Android Weird SD file contents.. 3 81
Organized HDD on one computer, moved HDD to another, contents are different, what happened? 9 105
Read about how to approach blogging and about ways to do it right. Stand out from the crowd and let your knowledge be consumed by a large audience. This article aims to explain how your blog should look like,  the most important things to do while b…
FAQ pages provide a simple way for you to supply and for customers to find answers to the most common questions about your company. Here are six reasons why your company website should have a FAQ page
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

776 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