Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

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

Posted on 2011-03-10
6
Medium Priority
?
912 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
6 Comments
 
LVL 16

Accepted Solution

by:
HagayMandel earned 2000 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
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

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…
A big percent of today’s marketing activity is performed through the online environment. The marketing strategies that have existed a decade ago no longer relate to what’s happening today. We’re currently facing a revolutionary era, called the digit…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …

604 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