1

I have below code

My data.csv

id, title,location_place_1,location_address_1
1,"test","Test","India"

/my-project/docroot/modules/custom/data_migrate/config/install/migrate_plus.migration.data.yml

id: data
label: Import data.
source:
  plugin: csv
  path: 'public://import/data.csv'
  delimiter: ','
  enclosure: '"'
  header_row_count: 1
  keys:
    - id
  ids:
    id:
      type: string
  column_names:
    0:
      id: 'Unique Id'
    1:
      title: Title

destination:
  plugin: entity:node
  default_bundle: data

process:
  title: title
  location:
    plugin: migration_lookup
    migration: paragraph_location_1
    source: id
  field_location:
    -
      plugin: deepen
      source: '@location'
    -
      plugin: sub_process
      process:
        target_id: '0'
        target_revision_id: '1'

migration_dependencies:
  required:
    - paragraph_location_1

Below is the code for /my-project/docroot/modules/custom/data_migrate/src/Plugin/migrate/process/Deepen.php

<?php

namespace Drupal\data_migrate\Plugin\migrate\process;

use Drupal\migrate\ProcessPluginBase;
use Drupal\migrate\MigrateException;
use Drupal\migrate\MigrateExecutableInterface;
use Drupal\migrate\Row;

/**
 * Transforms an array of values, into associative arrays with one key.
 *
 * E.g. ['alpha', 'beta'] becomes [[value => 'alpha'], [value => 'beta']].
 *
 * Use this plugin to preprocess a numeric array for other plugins,
 * that requires an associative array as input, such as the sub_process plugin.
 *
 * Available configuration keys:
 * - source: Source property.
 * - keyname: name of the key to be used for the associative sub-arrays,
 * defaults to 'value'.
 *
 * Example:
 *
 * @code
 * source:
 *   my_flat_array:
 *     - category1
 *     - category2
 * process:
 *   my_associative_array:
 *     plugin: deepen
 *     source: my_flat_array
 *   field_taxonomy_term:
 *     plugin: sub_process
 *     source: '@my_associative_array'
 *       target_id:
 *         plugin: migration_lookup
 *         migration: my_taxonomy_migration
 *         source: value
 * @endcode
 *
 * @see \Drupal\migrate\Plugin\MigrateProcessInterface
 *
 * @MigrateProcessPlugin(
 *   id = "deepen",
 *   handle_multiples = TRUE
 * )
 */
class Deepen extends ProcessPluginBase {

  /**
   * {@inheritdoc}
   */
  public function transform($value, MigrateExecutableInterface $migrate_executable, Row $row, $destination_property) {
    $keyname = (isset($this->configuration['keyname']) && is_string($this->configuration['keyname']) && $this->configuration['keyname'] != '') ? $this->configuration['keyname'] : 'value';

    if (is_array($value) || $value instanceof \Traversable) {
      $result = [];
      foreach ($value as $sub_value) {
        if (is_array($sub_value) || $sub_value instanceof \Traversable) {
          foreach ($sub_value as $val) {
            $result[] = [$keyname => $val];
          }
        }
        else {
          $result[] = [$keyname => $sub_value];
        }
      }

      return $result;
    }
    else {
      throw new MigrateException(sprintf('%s is not traversable', var_export($value, TRUE)));
    }
  }

}

For the paragraph

/my-project/docroot/modules/custom/data_migrate/config/install/migrate_plus.migration.paragraph_location_1.yml

id: paragraph_location_1
label: Paragraph location migration from CSV data
migration_tags:
  - paragraph

source:
  plugin: csv
  path: 'public://import/data.csv'
  delimiter: ','
  enclosure: '"'
  header_row_count: 1
  keys:
    - id
  column_names:
    2:
      location_place_1: 'Location - Place'
    3:
      location_address_1: 'Location - Address'

destination:
  plugin: 'entity_reference_revisions:paragraph'
  default_bundle: location

process:
  field_place:
    plugin: get
    source: location_place_1
  field_address:
    plugin: get
    source: location_address_1
migration_dependencies:
  required: {  }
  optional: {  }
dependencies:
  enforced:
    module:
      - data_migrate

It's making entry in to paragraph table. But some how it's not attaching to data content type

Can anyone point out where I'm going wrong?

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Browse other questions tagged or ask your own question.