0

I am migrating CSV data into Nodes and Paragraphs in Drupal 8.

I have field in CSV file which has JSON data, I have written a process plugin to convert JSON data to multi-dimensional array.

I would like to know a way to pass the each sub array to my another custom process plugin. Is there any such process plugin available to pipe sub array from parental array in core migrate or through migrate_plus module.

I am looking for something similar so that it passes only sub array into the final custom process plugin.

  pseudo_field_country_time_zones:
    - # plugin which converts JSON data to multi dimensional array.
      plugin: country_timezones_data_formatter 
      source: timezones
    - # not sure if this is correct plugin to process array.
      plugin: sub_process 
    -
      plugin: country_timezones_paragraphs

You might ask why cant you process the data in the country_timezones_paragraphs process plugin itself. That plugin is being used by other process, so dont want to change it. Other solution for me is to write another process plugin but it will be just code duplication with foreach loop added to country_timezones_paragraphs.

I believe there is already a plugin available either from migrate or migrate_plus module for this scenario , but not able to identify what it is.


$input_data = '[{"zoneName":"Africa Ceuta","gmtOffset":3600,"gmtOffsetName":"UTC+01:00","abbreviation":"CET","tzName":"Central European Time"},{"zoneName":"Atlantic Canary","gmtOffset":0,"gmtOffsetName":"UTC\u00b100","abbreviation":"WET","tzName":"Western European Time"},{"zoneName":"Europe Madrid","gmtOffset":3600,"gmtOffsetName":"UTC+01:00","abbreviation":"CET","tzName":"Central European Time"}]';

Decoded JSON array.

$decoded_array = var_dump(json_decode($input_data, TRUE));

Array
(
    [0] => Array
        (
            [zoneName] => Africa Ceuta
            [gmtOffset] => 3600
            [gmtOffsetName] => UTC+01:00
            [abbreviation] => CET
            [tzName] => Central European Time
        )

    [1] => Array
        (
            [zoneName] => Atlantic Canary
            [gmtOffset] => 0
            [gmtOffsetName] => UTC±00
            [abbreviation] => WET
            [tzName] => Western European Time
        )

    [2] => Array
        (
            [zoneName] => Europe Madrid
            [gmtOffset] => 3600
            [gmtOffsetName] => UTC+01:00
            [abbreviation] => CET
            [tzName] => Central European Time
        )

)

1 Answer 1

0

There's no such process plugin in core or migrate_plus. If you can't find what you need in those lists, you'll probably have to write a custom solution. You may want to check out SubProcess process plugin which may be able to work with your decoded array with minimal changes.

2
  • thank you for the response I tried the plugin: sub_process already but I am unable to get the correct syntax for the iteration, can you share the syntax for my array input ? Feb 5, 2021 at 13:00
  • You need to add a process: part to your sub_process plugin. To do that you really need to understand what you're trying to do with each array row. I'm not sure what pseudo_field_country_time_zones is or how it's used, but I don't think using pseudo fields is a suitable approach in this case. If you look at examples on SubProcess page, they all have a process step, and that needs a key like id or target_id where the value should go.
    – Beebee
    Feb 9, 2021 at 11:43

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.