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

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.

Post has a happy end - we were able to migrate files and attach to a node!

An example for super heroes

For this blog post I created a dummy Drupal 8 installation profile with way too much information about super heroes. The migration module can migrate some images along with some CSV data about them.

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:

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

In Drupal 7, because we wanted to prepare the value before populating the entity fields, we did it in a prepare method. In Drupal 8 we have process plugins.

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.

Conclusion

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.