Tell us about your project

Drupal 8 logo and one CVS sheet next to it with arrow symbolizing upload/import

One of the cool things Drupal offers is migration. You've probably had a situation where you had some data to migrate from a CSV file to your Drupal site. With the Migrate module, you can transfer all kinds of entities such as nodes, taxonomy, files, and users from CSV, XML, and JSON files.

In Drupal 8, the Migrate module comes with installation in the core. In this tutorial, I'll walk you through how to import some data from CSV to your Website. We'll be importing nodes of content type "Article", which contain some image files.

Step I - Prepare required modules

First of all, we'll need to enable the Migrate module ( if it's still not ) and install the Migrate Source CSV module, which enables us to import data from CSV files. You can find it here.

Step II - Create a .csv file

We'll take these fields from the Article content type:

  • Title
  • Body
  • Image

Our article.csv file will look like this:

ID,title,body,field_image
1,"Title 1","Some test text 1","picture1.jpg"
2,"Title 2","Some test text 2","picture2.jpg"
3,"Title 3","Some test text 3","picture3.jpg"
4,"Title 4","Some test text 4","picture4.jpg"

Step III - Create migration configuration .yml files

To understand better the following paragraph, I recommend you visit this page and read that article first.

As we are also importing image files, we'll have to create two separate .ymlfiles, one for image files, and another for importing nodes.

article_image.yml

id: article_image
migration_tags:
  - CSV
migration_group: null
label: Image importer
source:
  constants:
    source_base_path: /var/www/your_project/sites/default/files/import/article #absolute path where your images are currently located
    uri_file: 'public://article/image' #location where your images will be stored
  plugin: csv
  track_changes: true
  path: '/var/www/your_project/sites/default/files/articles.csv' #absolute location where your csv files are located
  header_row_count: 1
  keys:
    - field_image

process:
  source_full_path:
    -
      plugin: concat
      delimiter: /
      source:
        - constants/source_base_path
        - field_image
    -
      plugin: urlencode
  uri_file:
    -
      plugin: concat
      delimiter: /
      source:
        - constants/uri_file
        - field_image
    -
      plugin: urlencode
  filename: field_image
  uri:
    plugin: file_copy
    source:
      - '@source_full_path'
      - '@uri_file'
destination:
  plugin: 'entity:file'
migration_dependencies:
  required: {  }
  optional: {  }

'public://' refers to the file's location in your project, and that it should be in the public files directory ( this is your default files directory ).

Next, we need to create another .yml configuration file that will import all nodes with previously defined images.

articles.yml

id: articles_import
label: 'Articles import'
source:
  plugin: csv #here we define that we are using csv migrate plugin
  path: /var/www/your_project/sites/default/files/articles.csv #absolute location where your csv file is located
  delimiter: ','
  enclosure: '"'
  header_row_count: 1
  keys:
    - ID

process:
  type:
    plugin: default_value
    default_value: article #content type name we will create
  title: title #node fields
  body: body
  'field_image/target_id':
      plugin: migration
      migration: article_image #previously defined migration for uploading image files
      source: field_image
  uid:
    plugin: default_value
    default_value: 1 #uid of the user which will be defined as the author of the imported notes, can be excluded
 destination:
  plugin: 'entity:node' #here we define what entity type we are creating
migration_dependencies:
  optional:
    - article_image #this is optional, you do not need to insert this, but the name must be the same as the migration id for images

 

Step IV - Import config files for article images

Now we can import these configurations migrate files to our Drupal website.

Go to: admin/config/development/configuration/single/import

Here you can now import those .yml files. Choose Migration as "Configuration type" and paste your configuration.

Remember: You need to import article_image.yml first to successfully import images with your articles.

 

Step V - Run migration for article image

Now we need to run migration files to create those migrations.

Open terminal in your project's root folder.

You can run Drush-ms ( ms- refers to migrate-status ) if you want to check if your article_image has been imported.

With Drush-mi article_image you will create that migration. If everything is okay, you'll see a message like this:

 

Processed 4 items (4 created, 0 updated, 0 failed, 0 ignored) - done with 'article_image'

 

Step VI - Import config files for article nodes

Repeat step IV, but this time paste the configuration file articles.yml

 

Step VII - Run migration for article nodes

Again, you need to repeat a previous step, step V this time.

Run Drush-ms to see if your articles_import migration is correctly imported.

Then go on and run this migration Drush-mi articles_import

If everything goes well, you should get this message:

 

Processed 4 items (4 created, 0 updated, 0 failed, 0 ignored) - done with 'articles_import'

 

Additional information

There are plenty of other migration commands, which you can find here.

Update migration with this command:

Drush mi articles_import --update -y

You can find what other Drush commands are enabling for migrations using the command drush help.


Lazar Padjan