2

I would like to migrate custom user fields from an external USER database table to the custom tables of Drupal 7.

I created custom fields for my users (First Name, Last Name, ...) under administration/configuration/account settings and I can confirm that these fields are created if I go to administration/people/ and click "edit" next to each user.

I also know that for each individual custom field that I have created, D7 created a new table of the form "field_data_field_[FIELD_NAME]" (ie. field_data_field_first_name, field_data_field_last_name, ...)

The specific commands I would like to find more information about are:

MigrateDestinationUser::getKeySchema();
$this->destination = new MigrateDestinationUser();

I understand the above command is the default used to import users to the default "users" database.

How can I migrate my content to another D7 table? Such as migrating the First Name from the external table to field_data_field_first_name?

1 Answer 1

3

An example migration is here (not my own) and there are some followup comments in that GIST: https://gist.github.com/893241

What you are talking about is mapping old fields of data to new fields of data in drupal.

You do this by calling addFieldMapping() in your Migration class constructor. The migration __constructor() function typically has 3 big pieces.

  1. Where is the data from. Your $this->source() SQL query.
  2. What fields map from what to where -- per field.
  3. Is there any default values or further cleanup needed of data. By using addField('new_drupal_field', 'old_value')->default('is_published', TRUE); or for instance addField('new_drupal_field', 'old_value')->seperator(',') for more than 1 entry per row.

See documentation on these pages:

The whole migration class, and more importantly its constructor, is basically a recipe of what drupal does per-row of the migration.

So after you have your source $query (basically a SELECT field1, field2, field3 from OLD_TABLE) you call the following:

// this is like saying field_first_name = old_field1 
$this->addFieldMapping('field_first_name', 'old_field1');
8
  • i am trying that out right now. i have also noticed if i manually insert the values into field_data_field_first_name, the data will not be displayed under admin/people - edit..would you know why? i tried manual insertion because the data fields seemed simple
    – chlong
    Commented Mar 27, 2012 at 16:31
  • I'm not sure ... if you do manual insertion you would have to do user_save() or entity_save() with a user array. Here is another small Gist moving some "users" into drupal. Basically you just need to change the SQL to do your own query and change the field mappings as I described above (I dont htink you need anything in their .install file) gist.github.com/1051550
    – tenken
    Commented Mar 27, 2012 at 17:09
  • I saw that one too. What i mean by manual insertion is with a direct SQL INSERT statement.
    – chlong
    Commented Mar 27, 2012 at 17:17
  • Hi, I know what you meant by manual insertion using SQL. the field is not just 1 field in 1 table in Drupal. A "User" in drupal is an entity and there is alot of data in many tables about a user (with custom fields). It's easiest to use the Drupal API and user_save() or entity_save() to do it "by hand" or use Migrate as outlined in the Gist. If you're having trouble with the code try like the Node_Import module and get a CSV of your user data and import that. A user import like your trying todo is fairly easy with Migrate, once you understand the migration process.
    – tenken
    Commented Mar 27, 2012 at 17:32
  • 1
    you need the items described here: drupal.org/node/1006982 your info file needs: dependencies[]=migrate and list your files[]=moveToDrupal.inc. Your .module file needs the 1 function in it YOURMODULE_migrate_api() in doc page linked here. Your moveToDrupal.inc needs to make a class that extends Migration. Drush should then see it. BE SURE YOU'VE NAMED EVERYTHING EXACTLY CORRECT. These 3 things tell "migrate" your custom migration exists. You also need to enable this custom module in the drupal admin.
    – tenken
    Commented Mar 27, 2012 at 19:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.