2

In my CSV file, I am having columns by name summary_1,summary_2 and summary_3. I want all of it migrated to a field in Drupal field_summary. The field supports multiple values.

id: sections_tfr
migration_tags:
  - CSV
migration_group: tfr
label: 'Import sections'
source:
  plugin: csv
  path: /var/www/html/php/tfr/prepared/final/sections.csv
  delimiter: ','
  enclosure: '"'
  header_row_count: 1
  keys:
    - s_id
  column_names:
    -
      s_id: Id
    -
      part_no: Part
    -
      sections: Section
    -
      summary_1: Summary
    -
      summary_2: Summary
    -
      summary_3: Summary

process:
  name: sections
  description: heading
  field_summary: summary_1
  field_summary: summary_2
  field_summary: summary_3
  weight: s_id  
destination:
  plugin: 'entity:taxonomy_term'
  default_bundle: sections
  migration_dependencies: null

2 Answers 2

4

It took a while for me to solve the issue. However, I was able to manage the same without having to write additional plugin or module. I solved the issues in two steps:

  1. First used the plugin concat to concatinate all 3 required fields to one.
  2. Then used the plugin explode to pass the value to the same field.

Yaml Below:

id: sections_tfr
migration_tags:
  - CSV
migration_group: tfr
label: 'Import sections'
source:
  plugin: csv
  path: /var/www/html/php/tfr/prepared/final/sections.csv
  delimiter: ','
  enclosure: '"'
  header_row_count: 1
  keys:
    - s_id
  column_names:
    -
      s_id: Id
    -
      part_no: Part
    -
      sections: Section
    -
      summary_1: Summary
    -
      summary_2: Summary
    -
      summary_3: Summary

process:
  name: sections
  description: heading  
  field_summary:
    -
      plugin: concat
      source:
        - summary_1
        - summary_2
        - summary_3
      delimiter: ','
    -
      plugin: explode
      limit: 100
      delimiter: ','
  weight: s_id
destination:
  plugin: 'entity:taxonomy_term'
  default_bundle: sections
  migration_dependencies: null
migration_dependencies: null
3

I was struggling with this very same issue. Even though the official migration docs suggest that multivalued fields will be handled by the pipeline automatically, it didn't happen for me(at least at the time of writing this). I ended up extending the entity_generate plugin provided by migrate_plus to generate multiple entities. Here's the plugin code:

Namespace Drupal\my_module\Plugin\migrate\process;

use Drupal\migrate_plus\Plugin\migrate\process\EntityGenerate;
use Drupal\migrate\MigrateException;
use Drupal\migrate\MigrateExecutableInterface;
use Drupal\migrate\Row;

/**
 * This plugin generates entities within the process plugin.
 *
 * @MigrateProcessPlugin(
 *   id = "entity_generate_multiple",
 *   handle_multiples = TRUE
 * )
 *
 */
class EntityGenerateMultiple extends EntityGenerate {

  /**
   * {@inheritdoc}
   */
  public function transform($value, MigrateExecutableInterface $migrateExecutable, Row $row, $destinationProperty) {
    // Input value should be an array
    if(!is_array($value)) {
        throw new MigrateException('Input should be an array.');
    }
    $result = [];
    foreach($value as $val) {
        $result[] = parent::transform($val, $migrateExecutable, $row, $destinationProperty);
    }

    return $result;
  }
}

And here's how I used it,

  field_tags:
    -
      plugin: get
      source:
        - tag_1
        - tag_2
        - tag_3
    -
      plugin: entity_generate_multiple
      value_key: name
      bundle_key: vid
      bundle: tags
      entity_type: taxonomy_term

Hope that helps.

1
  • 1
    I found a workaround using existing plugin, see it below. Let me know if it works for you too.
    – esafwan
    Nov 16, 2017 at 21:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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