Solved

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

Posted on 2011-03-10
6
853 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
LVL 16

Expert Comment

by:HagayMandel
Comment Utility
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
Comment Utility
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
Comment Utility
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

A Knowledge Base That Stays Up-to-Date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

In this article, we will look at our content management system as a whole, to get an idea of how we will start to manage these content items.
This article will shed light on the latest trends when it comes to your resume building needs. For far too long, the traditional CV format has monopolized the recruitment market.
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

771 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now