2

How do I migrate a sequence of data into a multi-value field?

I am learning D8 migration and working on a large CSV of historic census data from around the world. Example below:

id,Country Name,Country Code,Indicator Name,Indicator Code,1960,1961,1962,1963,1964,1965,1966,1967,1968,1969,1970,1971,1972,1973,1974,1975,1976,1977,1978,1979,1980,1981,1982,1983,1984,1985,1986,1987,1988,1989,1990,1991,1992,1993,1994,1995,1996,1997,1998,1999,2000,2001,2002,2003,2004,2005,2006,2007,2008,2009,2010,2011,2012,2013,2014,2015,2016,2017,2018
1,Aruba,ABW,"Population, total",SP.POP.TOTL,54211,55438,56225,56695,57032,57360,57715,58055,58386,58726,59063,59440,59840,60243,60528,60657,60586,60366,60103,59980,60096,60567,61345,62201,62836,63026,62644,61833,61079,61032,62149,64622,68235,72504,76700,80324,83200,85451,87277,89005,90853,92898,94992,97017,98737,100031,100834,101222,101358,101455,101669,102046,102560,103159,103774,104341,104872,105366,105845

My migration yml file is as follows:

uuid: 6c54b8f4-96c7-4678-8919-b20c8b318c82
langcode: en
status: true
dependencies: {  }
id: World_Census_Data
class: null
field_plugin_method: null
cck_plugin_method: null
migration_tags:
  - CSV
migration_group: random_migrations
label: 'International census csv data'
source:
  plugin: csv
  path: /var/www/random-migrations/modules/custom/random_migrations/sources/census-58-years.csv
  ids:
    - id
  delimiter: ','
  track_changes: true
process:
  type:
    plugin: default_value
    default_value: census
  title: 'Country Name'
  field_country_code: 'Country Code'
  field_count:
    plugin: array_build
    default_value: 0
    value:
      - 1960
      - 1961
destination:
  plugin: 'entity:node'
migration_dependencies: {}

Instead of manually creating an individual field for each year, I created the field_count field which is an Interger field set to allow unlimited entries. My idea is to import all the population numbers from 1960 to 2018 as new entries of field_count for each country.

My problem is the population data is not being imported. Can anyone advice if my proposed solution possible and if it is, what I need to change/fix in my yml to get my migration working?

Help, please.

1

I figured it out. The issue was the plugin. Rather than * array_build*, I should have been using the get plugin, as follows:

process:
  type:
    plugin: default_value
    default_value: census
  title: 'Country Name'
  field_country_code: 'Country Code'
  field_count:
    plugin: get
    default_value: 0
    source:
      - 1960
      - 1961
      - 1962
     .
     .
     .

That migrates the value of the array indexed by those years into the field_count field. And because the field has multiple cardinality, each row is written in as a new value.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.