I'm trying to populate a new Drupal 8 (8.3.2) site with content from a CSV file, using core's Migrate module, Migrate Source CSV, Migrate Tools and Migrate Plus. I'm able to import each node successfully with a single image, but I'm unsure of how to migrate multiple images into the node's multi-value image field.
My module currently looks like this:
id: product_data
label: Products
migration_group: product_migration
migration_tags:
- node
- product
source:
plugin: csv
path: 'public://import/products/products.full.csv'
header_row_count: 1
keys:
- product_sku
constants:
bool_0: 0
bool_1: 1
uid_root: 1
restricted_html: restricted_html
destination:
plugin: 'entity:node'
default_bundle: product
process:
title: product_name
field_sku: product_sku
sticky: constants/bool_0
promote: constants/bool_1
uid: constants/uid_root
'field_image/target_id':
plugin: migration
migration: product_image
source: product_full_image
migration_dependencies:
optional:
- product_image
dependencies:
enforced:
module:
- custom_migration
And to create the image entities:
id: product_image
label: Images associated with a product.
migration_group: product_migration
migration_tags:
- product
- file
- image
source:
plugin: csv
path: 'public://import/products/products.full.csv'
header_row_count: 1
keys:
- product_full_image
fields:
product_full_image: Name of the image file associated with the program.
constants:
file_source_uri: public://import/products/old-images
file_dest_uri: 'public://program/image'
destination:
plugin: 'entity:file'
process:
file_source:
-
plugin: concat
delimiter: /
source:
- constants/file_source_uri
- product_full_image
-
plugin: urlencode
file_dest:
-
plugin: concat
delimiter: /
source:
- constants/file_dest_uri
- product_full_image
-
plugin: urlencode
filename: product_full_image
uri:
plugin: file_copy
source:
- '@file_source'
- '@file_dest'
dependencies:
enforced:
module:
- custom_migration
And the csv file is formatted like this:
product_sku,product_name,product_full_image
C242,couch,Blue Couch,"image1.jpg,image2.jpg"
C243,chair,Red Chair,image3.jpg
I've tried to work with the expload and iterator plugins, but honestly I'm totally lost here. Any ideas would be greatly appreciated.
I was able to figure this out -- hopefully this can help others in the same boat. My final migration looked like this:
Main module:
id: product_data
label: Products
migration_group: product_migration
migration_tags:
- node
- product
source:
plugin: csv
path: 'public://import/products/products.full.csv'
header_row_count: 1
keys:
- product_sku
constants:
bool_0: 0
bool_1: 1
uid_root: 1
restricted_html: restricted_html
destination:
plugin: 'entity:node'
overwrite_properties:
- field_image
default_bundle: product
process:
field_image:
-
plugin: explode
delimiter: ;
source: image
-
plugin: migration
migration: product_image
no_stub: true
migration_dependencies:
optional:
- product_image
dependencies:
enforced:
module:
- custom_migration
The 'products.full.csv' file referenced above lookes like this:
product_sku,product_name,image
C242,Blue Couch,image1.jpg;image2.jpg;image3.jpg
C243,Red Chair,image4.jpg
To import and create entities for all of the images:
id: product_image
label: Import all product images.
migration_group: product_migration
migration_tags:
- product
- file
- image
source:
plugin: csv
path: 'public://import/products/images.csv'
header_row_count: 1
keys:
- image
fields:
new_image: Name of the image file associated with the program.
constants:
file_source_uri: public://import/products/images
file_dest_uri: 'public://program/image'
destination:
plugin: 'entity:file'
process:
file_source:
-
plugin: concat
delimiter: /
source:
- constants/file_source_uri
- new_image
-
plugin: urlencode
file_dest:
-
plugin: concat
delimiter: /
source:
- constants/file_dest_uri
- new_image
-
plugin: urlencode
filename: new_image
uri:
plugin: file_copy
source:
- '@file_source'
- '@file_dest'
dependencies:
enforced:
module:
- custom_migration
The 'images.csv' file referenced above only has one column and is just a straight list of all images to create entities for, one per line:
image
image1.jpg
image2.jpg
image3.jpg
image4.jpg
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With