Blob of contradictions

Main menu

You are here

Migrating Content Part 2: Nodes

Fri, 03/25/2011 - 10:12 -- btmash

(Updated November 10, 2011 - Updated information about file actions. Thank you Patrick Thurmond!)
(Updated June 4, 2011 - File Handler Compatibility with Migrate 2.1)
(Updated June 8, 2011 - Image to show how to write out your destination field names)Updated August 29, 2012: I wrote out a blog post on the new image/file handler changes from Migrate 2.4+ a few weeks ago which you can read about here. I've not updated this blog post with those changes so take a bit of what you learn from these blog posts, and add in all the things from the new one for anything file related (or just look at the new one since I link out to the new reference codebase from there as well). and hopefully it helps :)

Its been a while since I wrote about using the migrate module to migrate content from various sources. The last time, I covered migrating users into Drupal and this time, I will write out how to migrate content into drupal as nodes. Because the migrate module is so flexible, it makes doing such things quite easy.

And this class will consist of two functions - one is the class constructor while the other is for massaging/adding extra information on a per row basis.

/**

* Class Constructor

*/

publicfunction __construct(){

...

}

/**

* Add any additional data / clean up data for the current row / node that will be added/updated.

*/

publicfunction prepareRow($current_row){

...

}

Let's start by defining the class constructor.
We need to create the mapping information (for more detail on how the mapping information works, my previous article on migrating users would be a recommended read as I will be diving headfirst into posting the code). Please note that I had already defined some constants before hand: MY_MIGRATION_DATABASE_NAME is the name of the database, MY_MIGRATION_FILES_DIRECTORY is the path to my files directory.

From the above code, you should see that it is very similar to the mappings done for the user in the prior link. We are simply defining more tables to join against the initial table and columns that we want pulled in. However, there is one new component that has now been added: highwater. Highwater is a more recent concept that has been added to the migrate module which provides you with the ability to update existing content that had been migrated into your new Drupal site. This means that content does not have to be rolled back and migrated back in! You define which column from your table will be the 'highwater' mark to denote whether or not a piece of content needs to be updated. The other piece that you have to provide is your sql query has to also be sorted by the same highwater column.

With that out of the way, we are now ready to start mapping fields. As a reference, this is what part of my migration screen looks like for migration of various pieces of node info (this includes node title, status, uid, etc along with fields).
Most of the values below are the same as the way in which mappings were done for the users.

// Make the mappings

$this->addFieldMapping('title','title');

$this->addFieldMapping('is_new')->defaultValue(TRUE);

$this->addFieldMapping('uid','uid');

$this->addFieldMapping('revision')->defaultValue(TRUE);

$this->addFieldMapping('revision_uid','uid');

$this->addFieldMapping('created','created');

$this->addFieldMapping('changed','changed');

$this->addFieldMapping('status','status');

$this->addFieldMapping('promote','promote');

$this->addFieldMapping('sticky','sticky');

$this->addFieldMapping('comment','comment');

$this->addFieldMapping('language')->defaultValue('und');

$this->addFieldMapping('path')->issueGroup(t('DNM'));

$this->addFieldMapping('pathauto_perform_alias')->defaultValue('1');

$this->addFieldMapping(NULL,'name');

$this->addFieldMapping(NULL,'vid');

$this->addFieldMapping(NULL,'type');

$this->addFieldMapping(NULL,'language');

$this->addFieldMapping(NULL,'moderate');

$this->addFieldMapping(NULL,'tnid');

$this->addFieldMapping(NULL,'translate');

But the migration wouldn't be a migration if we only have simple node table column values to migrate and had actual fields. The migrate module has currently mapped out all core fields (and migrate extras is mapping out fields defined by other modules though work is ongoing). In the case of text fields (or text field with summary/whatnot), there are now arguments to migrate that content into your new Drupal site.

The first argument in the body_arguments is to see which field would map to a summary text field (if there was any). In the event that we did have one, it would be by defining where to find the source field:$arguments = MigrateTextFieldHandler::arguments(array('source_field' => 'excerpt'));. The second field is which filter you wish to apply to the content (so if you know the machine name of the filter you wish to use or pull it dynamically, you may do so in the same way as for the first argument). The final argument is the language of the text field.

The migrate module also provides the ability to import file fields and have them get attached to nodes.

Update as of June 4, 2011: There is a lot less going on in the code above - we get going to be getting multiple files and assigning attributes to them for the import. For the arguments, they consist of:

If the first item is null, then defining it in your values passed in will need to define a path to the file (this will all get defined during the prepareRow() implementation. Also for this file definition, things are *MUCH* simpler than using the separator method from Migrate 2.0 whereby a PHP array is defined (and JSONified using drupal_json_encode) for migrate. For the array, we define a few things:

Since the right_side_images above are now an array of images, you can have 0, 1, 10, 20, N files associated with that particular field! And with all that, we have our various fields defined for migrating content from another database into nodes!
I hope all of this is helpful. If something doesn't make a lot of sense, leave a comment! I'll try and improve this documentation. And next time, I'll post how to migrate an event node with dates, taxonomy terms, and attached files.