Quick Tips for a Drupal 7 to Drupal 8 Migration
Here are a few quick tips for a Drupal 7 to Drupal 8 migration. I will add more over time; admittedly it’s pretty lame right now!
For general Drupal 8 Migrate API tips, you may also want to check out my other post: Drupal 8 Migrate – Tips and Tricks
Source Plugins
If you’re using a source plugin like d7_user or d7_node it will help you understand what’s happening if you view the source code for the plugin. Pay close attention to the query(), prepareRow(), and fields() methods.
You will find the source plugins by browsing the core code. If, for example, you were looking for the d7_user source plugin you would browse to /core/modules/user/src/Plugin/migrate/source/d7/User.php . If you want to find the associated migration template you would browse to /core/modules/user/migrations/d7_user.yml . For d7_node you’d browse through the filesystem using the same pattern but with node instead of user.
Modifying the Source Plugin Query
I have a few posts on this already:
- Only Migrating Published Nodes in a D7 to D8 Migration
- Using GROUP_CONCAT to Combine Rows in a Drupal Query
Modifying a Row
You can implement your own prepareRow() method to alter the source plugin’s easily. See Only Migrating Published Nodes in a D7 to D8 Migration first, which covers extending a source plugin. Instead of defining a query() method you’ll define a prepareRow() method. Call the parent’s prepareRow() after you’ve done your manipulations. Here’s a simple example:
1 2 3 4 5 6 7 8 9 |
/** * {@inheritdoc} */ public function prepareRow(Row $row) { $created = $row->getSourceProperty('created'); $row->setSourceProperty('php_pubdate', date('Y-m-d', $created)); return parent::prepareRow($row); } |
Field by Field Method
I have found myself executing a migration repeatedly with --limit="10 items" and --update as I try to get each field working (some fields are more difficult than others). What seems to work well is keeping an eye on the database field tables as you do this. My process is, using a d7_user migration and field_user_fname as an example:
- Add the field to the migration yml file
- Re-install/import/whatever the configuration to introduce the change
- drush migrate:import mysite_users --limit="10 items" --update
- Refresh the user__field_user_fname table in Sequel Pro (the application I use to browse the DB)
- Look for field data. If no field data appears, or it doesn’t look right, I adjust the migration yml code, or the source plugin (if I’ve extended it). Then, I repeat this process until I start seeing the expected data in the table db table.