Import your WCI Content in Drupal
I wanted to blog about something exciting and relative to the current hour such as "who will win the coming presidential race to the Whitehouse", especially that the first presidential debate of 2012 is looming (personally I think Jim Lehrer is pretty lame and would have preferred to see Oprah Winfrey conducting these debates.) But my friend Vasanth hinted that the topic isn’t quite inline of Function1’s blogging policy and that we should be politically correct (whatever that means.) My argument that Whitehouse.gov is built in Drupal and that we aren’t really deviating from a technical topic landed on deaf ears! Oh well; back to the technical jargon :-) We posted a couple of blogs on Drupal and its merits, and why it is a strong alternative to WebCenter Interaction (WCI). In that same blog, we discussed one route to export WCI’s knowledge directory documents and each document’s properties using a JSON feed. And in a separate blog, we explored how to export Publisher content as an XML feed. So, now, let’s discuss how to import that content in Drupal. Drupal is content driven, that is the rendered HTML is populated from a number of different content types stored in a database. The most basic content is, well, of type “Basic page” that has a couple of fields: a title and a body. Odds are your web presence is more than just static content; you might have pages with content such as products, vendors, blogs, press releases, events, etc. These different content types have different data elements or fields. So the first thing when considering Drupal as a Web Content Management (WCM) is to structure your site first in terms of its different content types. So, for example, building off our previous example of the employees XML feed that has these data elements:
<?xml version="1.0" encoding="UTF-8" ?> <employees> <employee> <last_name>Doe</last_name> <first_name>John</last_name> <birth_date>01/01/1980</birth_date> <email>john.doe@email.com</email> <telephone>77-777-7777</telephone> <gender>male</gender> <address>1600 Pennsylvania Avenue</address> <city>Washington</city> <state>DC</state> <zipcode>20005</zipcode> </employee> </employees>
We would first create an "employee" content type in Drupal and then write the PHP script to import the XML feed into the “employee” Drupal node (make sure to save the script somewhere inside your Drupal install directory. The PHP import script would be:
<?php //Read the XML content into a string $data = file_get_contents('employees.xml'); //Treat the $data string as an XML object to access its elements $xml = new SimpleXMLElement($data); //Iterate through each XML element of type 'employee' and assign it to $employee foreach($xml->employee as $employee) { //$user is a Drupal core object representing the user currently logged in global $user; //stdClass is PHP's generic empty class (kind of like 'Object' in Java) $node = new stdClass(); //Node will be created by current user id $node->uid = $user->uid; //Set node type to 'employee' $node->type = 'employee'; //node_object_prepare is required to set $node properties node_object_prepare($node); //Let's set some default properties $node->language = LANGUAGE_NONE; //Set the Drupal field 'employee_last_name' to the 'last_name' attribute in the XML $node->employee_last_name => $employee->last_name; //Repeat for all the XML attributes $node->employee_first_name => $employee->first_name; $node->employee_email => $employee->email; //If 'gender' is a term reference where 'M' is male and 'F' is female then if (strtolower($employee->gender) == 'male') { $node->employee_gender = 'M'; else $node->employee_gender = 'F'; } $node->employee_telephone = $employee->telephone; $node->employee_address = $employee->address; $node->employee_city = $employee->city; $node->employee_state = $employee->state; $node->employee_zipcode = $employee->zipcode; //Need to format the date field $node->employe_birth_date[$node->language][0]['value'] = gmdate('Y-m-d H:i:s', strtotime($employee->birth_date)); //There are other Drupal node properties that you can also set before saving the node $node->status = 0; //default is 1 for published or not $node->promote = 1; //default is 0 meaning not promoted to front page //Save the node node_save($node); echo "Employee node with node id " $node->nid . " was successfully savedn"; } ?>
Obviously, you can import your data in Drupal from different data stores (e.g a database table(s), flat file, etc.) but the emphasis here is that we are importing WCI content; therefore XML or JSON might be the most appropriate industry standard intermediary formats. Thanks.
- Log in to post comments