Drupal-8-manina is at its highest. Modules are being ported, blog posts are being written, and new sites are being coded, so we in Gizra decided to join the party.
We started with a simple site that will replace an existing static site. But we needed to migrate node attachments, and we just couldn’t find an existing solution. Well, it was time to reach out to the community
Any example of #Drupal 8 migration of files/ images out there? (including copy from source into public:// )— Amitai Burstein (@amitaibu) April 8, 2016
A few minutes after the tweet was published, we received a great hint from the fine folks at Evoloving Web. They were already migrating files into Drupal 8 from Drupal 7, and were kind enough to blog post about it.
However, we were still missing another piece of the puzzle, as we also wanted to migrate files from an outside directory directly into Drupal. I gave my good friend @jsacksick a poke (it’s easy, as he sits right in front of me), and he gave me the answer on a silver platter.
An example for super heroes
If you’ll look closely you can see that I’ve attached an SQL dump with raw tables. This raw table will be the source that eventually will migrated into nodes, and you can read here how it was created with csv2sql.
Basic structure of migration
The description of the mapping between the source table and the destination node type is in a yaml file.
Let’s go over the interesting parts of the
process: type: plugin: default_value default_value: super_heroes uid: plugin: default_value default_value: 1 title: _title field_image: source: _image plugin: file_import field_alter_ego: _alter_ego 'body/value': _description
For example, the
default_value plugin will populate the (configurable) field of the entity with a raw value like the name of
a content type or a user ID, in case we are migrating all the nodes with the same author (e.g. user ID 1).
But we can, of course, have our own logic. In the transform method of the process plugin we can massage our data and return any value which will eventually populate the field.
In our case, the
transform method is responsible for adding the new file into Drupal using
file_unmanaged_copy and friends.
Some of the know hows and best practices are still missing from Drupal 8. But they can and should be re-learned and re-published. So remember kids, if you ever feel frustrated about not finding a solution, always reach out to smart community members and then write a post about it, so everybody can profit.