XML-D8 Migration: Very basic migration by just using YML file

As we are working XML to Drupal 8 migration project, so we just want to explore how the very basic migration looks like!

We have written a custom module to define mapping between XML data and Drupal 8 entity.

Like any other module in D8, we need .info.yml file which looks like below,

https://github.com/amitgoyal/migrate_xml_d8_custom/blob/master/migrate_xml_d8_custom.info.yml
In the .info.yml file, we have added dependencies for migrate_plus and migrate_tools modules. Migrate Plus module is required to do migration from sources like XML and JSON. Migrate Tools module provides tools for running and managing Drupal 8 migrations.

As we are migration from XML so we have created a sample XML file which contains ID, Title and Body fields. It looks like below,


Lastly and most importantly, for this very basic migration, we just needed one .yml file in config/install folder for our migration. This .yml file defines Source (XML file and fields in it), Destination (Drupal entity) and Field mappings (Process plugins). It looks like below,

https://github.com/amitgoyal/migrate_xml_d8_custom/blob/master/config/install/migrate_plus.migration.pages_content.yml

We have placed the source XML file (pages_content.xml) in Drupal root folder. After enabling the module, we have executed the migration using Drush like below,


XML file contained 3 records which have been migrated to Drupal site using Drush. We can see below that 3 records have been created on Drupal 8 site.



The source code is also available on Git repository.

Please note that in case you make any changes in .yml file under config/install/ after enabling the module then you have to uninstall the module first and then enable it again. Otherwise your changes will not be reflected. In such cases you also need to delete a record from config table using below SQL command,

DELETE FROM `config` WHERE `name` LIKE '%pages_content%';

To avoid deleting the record manually, we can use hook_uninstall() in .module file.

You can also explore more on migration from various resources in migrate_example_advanced module under migrate_tools module.

Comments

Popular posts from this blog

Simple Meditation

Custom MySQL Database to Drupal 8 Migration