All Posts in form api

May 11, 2012 - Comments Off on Building new sites… with some old data

Building new sites… with some old data

The files are IN the computer!

When building a new site for a client, migrating data from an old site or system can be a daunting task. There are several excellent modules such as feeds_import which can help move data that is already online in one form or another via rss/xml and so on. Sometimes there is content which needs to get onto the site that lives off-line or for other reasons isn't compatible with an existing import module- for that you can write some code to help out.

For one project we had to import a list of users which came from a CSV export from a spreadsheet. We really just needed users in the system to be able to assign them as authors of content. The following code is what we used to read in the CSV file and create the users. Any CCK field can be populated with this, here we are adding First, Middle and Last name fields before submitting.
This script relies on Drush and the Forms API to bootstrap drupal and feed it the form submission.

As with all things Drupal, there are more ways to accomplish a task than there are tasks to accomplish.

* Import users via a CSV
// Initialize a counter to track the number of users processed
$i = 0;
// Check for the .csv file in the particular directory. We create a <site>_util directory
// to put these sorts of scripts.
if (($handle = fopen("./sites/all/modules/custom/site_util/new-users.csv", "r")) !== FALSE) {
// While there are rows of data in the file, keep looping through.
while (($data = fgetcsv($handle, 0, "," )) !== FALSE) {
// Read in and sanitize data from the CSV file.
// Here we assign each column to a variable.
$fname = utf8_encode(trim($data[0]));
$mname = utf8_encode(trim($data[1]));
$lname = utf8_encode(trim($data[2]));
$email = trim($data[3]);
// Create random 8 character password.
$pass = user_password();

// Initialize the $form_state array which will be passed to drupal_form_submit.
$form_state = array();

// Tell drupal_form_submit what operation this form is performing
$form_state['values']['op'] = t('Create new account');

// Drupal 7 requires this second password field to create users.
$form_state['values']['pass']['pass1'] = $pass;
$form_state['values']['pass']['pass2'] = $pass;

// Populate the name and email elements of $form_state.
$form_state['values']['field_profile_fname']['und'][0]['value'] = $fname;
$form_state['values']['field_profile_mname']['und'][0]['value'] = $mname;
$form_state['values']['field_profile_lname']['und'][0]['value'] = $lname;
$form_state['values']['mail'] = $email;

// Build a human readable Username. We are using email as the primary login.
// Since the users do not always have a middle name we will use a
// a ternary operation to prevent adding a second space between the
// first and last name.
$form_state['values']['name'] = $fname . ' ' . ($mname ? $mname . ' ' : '') . $lname;

// Since we're running on the command line, we'll add some status information.
print('Adding: ' . $form_state['values']['name']. "n");

// Finally, we submit the built $form_state array to Drupal's user_register_form.
drupal_form_submit('user_register_form', $form_state);

// Increment the counter.
print("Processed: " . $i . " users.");

Save this code to a file and then run execute it with drush:
% drush scr script.php

Thats it. It should import your users and report at the end.
You'll need to make sure the data going in is sensible, valid emails, names etc.

Published by: chazcheadle in The Programming Mechanism
Tags: , , , ,