Examples of Import/Export API use

Events happening in the community are now at Drupal community events on www.drupal.org.
alanburke's picture

Hi All,

I am currently trying to get my head around this API.

I need to import a CSV file into Drupal.. simple enough really.

What I'm looking for is some example of use of the ImportExportApi in use in some other modules.

I find it really helps to see the code in action somewhere, to get a good feel for how it works.

So apart from the Module itself, can anybody point me int the right direction?

Later
Alan

Comments

I'm also working on this

phil r's picture

I'm also working on this kind of thing.

I have some data that I want to import into drupal to auto-generate cck nodes. I've successfully used the UI to manually import data into cck nodes by first exporting from a cck node, manually editing the exported structure, and importing that data.

I want to perform this exact task, but automatically, via a module which will do the following on each page load.

The module already does
- get new data
- translate data into acceptable input form

What I need
- run inportexportapi to import translated data to cck content type

I can't find any documentation on how to utilize the importexportapi hooks, so I'm browsing the importexportui.module to see how importexportapi hooks function. Any help or documentation on this would be appreciated

Ok, the specific

phil r's picture

Ok, the specific documentation is in the download itself under /docs/developer/topics.

From the docs, the two main functions that you need to call are importexportapi_get_data and importexportapi_put_data. These functions are generally called together, with the get function returning values to imput into the put function.

The following is the syntax.

// $entity_list is an array of content types available (some node type, cck type, etc...eg. role, page, cck type)
// $engine is the data format tranlsation (db, csv, xml, or a build your own)
// array() is an array of any additional information to pass
$data = importexportapi_get_data($entity_list, $engine, array('foo'));

// $data is the returned values from importexport_get_data
// Null is the put engine, see $engine above
// array() is the same as above
$ret = importexportapi_put_data($data, NULL, array('foo'));

To find a list of available entities, use importexportapi_get_def function

// You can pass a specific key to specify what you want (eg. role, page, etc.)
$def = importexportapi_get_def();

These are great functions in and of themselves. It's just a bit confusing to see where the actual data is placed, and how fields (content type fields, specifically) are being compared and populated.

For instance, I want to create new roles. I've exported the data in csv form from 'user roles' using the nifty export UI. I tweak this to add some new user roles, and write a script to import it.

// My user role file looks like:
// "rid","role-name","perm-rid","perm"
// "1","anonymous user","",""

// Read the file contents into a scalar
$new_data = get_file_contents($user_role_file);
// Alternatively you could use file($filename) to read to an array

// the key for the entity 'user_role' is actually role.  user importexportapi_get_def to check entity_role keys
$entity_list = array("role");
$engine = "csv";

// call get and put functions
// I think the NULL value is drawn the the entity's metadata value #source, which would of course come
// from 'user role' source, which would of course be the db
$data = importexportapi_get_data($entity_list, $engine, array(??????????????));
$ret = importexportapi_put_data($data, NULL, array(?????????????));

So what data do these functions want? How should it be formatted? How does it get compared to the entity's values?

Ok, so here's what I

phil r's picture

Ok, so here's what I learned.

<?php
// This example is to input some data from a csv file to the drupal database 'user role' node type.  As before, I used the ImportExportUI export gui to export an example of how the 'user role' data is formatted

// My user role file looks like:
// "rid","role-name","perm-rid","perm"
// "1","anonymous user","",""

// declare your arrays and vars
$entity_list = array ("role");
$raw = array();

// Get the contents of the role file and write it, as a string, to a var
$file_data = file_get_contents ("$path/$filename");

// The variables are arbitrary, but in this case, the first one will get data from the csv, the next one will put data in the db
$engine    = "csv";
$engine_db = "db";

// Invoke the get_def function to view all the data definitions that the API sees, and more importantly, to get the roles csv specific title
$def = importexportapi_get_def(NULL, TRUE);

// The $def array contains all the definition data for all the available content types.  You can loop through these, like was done in the ImportExportAPI, or, if you're like me and are making a specific module for one content type, you can define the name explicitly.  The $def['role'] returns an array of data about 'role', #csv_plural is a key created by ImportExportAPI which assigns a unique identifier for each content type.  For this case, it is assigned 'roles'.
$csv_plural = $def['role']['#csv_plural'];

// Assign the scalar data into the $raw array with the csv_plural as the key
$raw[$csv_plural] = $file_data;

// That's all you need to do!  Invoke the get and put functions, determine the entity (role), engine (csv), array('roles' => '$scalar')
$data = importexportapi_get_data($entity_list, $engine, array('raw' => $raw));
$ret = importexportapi_put_data($data, $engine_db);
?>

Thanks for the code! Are there any plans for porting it to 5.0?

Import/Export API Port 5.0

coupet's picture

Port 5.0 is available now.

Darly

Support for Category types?

jaydub's picture

I've started to get this importexportapi module working but was wondering if it's possible to import/export Jaza's own Category module content types. I don't see anything in the standard definitions directory and before trying to roll my own I was hoping maybe someone had already done it. Alternatively if there's a way to use the taxonomy content type (vocabularys) and automagically convert to category module containers and categories via the category module api.

any advice appreciated.

Content migration, import, and export

Group organizers

Group notifications

This group offers an RSS feed. Or subscribe to these personalized, sitewide feeds: