Migrate source csv

WE HAVE MOVED!

Please visit our new Drupal Website at https://www.xenyo.com我們的新網頁設計公司網頁.

Using migrate source csv module, you can import the following types :
 
1. Title, body with text format, body summary
2. Date field with different format
3. Taxonomy (create if not exist)
4. Multiple images per field (need a separate csv file for images, file name as key value)
5. Multiple paragraphs per field (need a separate csv file for paragraph, id as key value)
6. Multiple values per field
7. uid (user reference)
8. created, updated date (timestamp)
 
# enable your custom migrate module first.
 * drush en custom_migrate
 
 # If you have changed the config file, you need to import the config to take effect
 * drush config-import --partial --source=modules/custom/custom_migrate/config/install/ -y
 
# first we need to import images and paragraph data first so that these items can be refereced by migrate_test.
 * drush migrate-import migrate_images --update
 * drush migrate-import migrate_paragraph --update
# run migrate_test
* drush migrate-import migrate_test --update
 
If you got php error during import, you can clean up the queue manually using sql query
 * delete from key_value where collection = "migrate_status"
 
 
-- /modules/custom/custom_migrate/custom_migrate.info
 
name: Custom Migrate
type: module
description: Custom Migrate
core: 8.x
package: Migration
dependencies:
  - migrate_source_csv
  - migrate_plus
  - migrate_tools
 
 
# we need “key_wrapper” plugin in order to import paragraph field.
 
-- /modules/custom/custom_migrate/src/Plugin/migrate/process/KeyWrapper.php
 
<?php
namespace Drupal\custom_migrate\Plugin\migrate\process;
 
use Drupal\migrate\ProcessPluginBase;
use Drupal\migrate\MigrateException;
use Drupal\migrate\MigrateExecutableInterface;
use Drupal\migrate\Row;
 
/**
 * Determine the most recent entity revision id given an entity id
 *
 * @MigrateProcessPlugin(
 *   id = "key_wrapper"
 * )
 */
class KeyWrapper extends ProcessPluginBase {
 
  /**
   * {@inheritdoc}
   */
  public function transform($value, MigrateExecutableInterface $migrate_executable, Row $row, $destination_property) {
    $new_value = array(
        'key_wrapper' => $value,
    );
    $value = $new_value;
    return $value;
  }
}
 
 
-- /modules/custom/custom_migrate/config/install/migrate_plus.migration.migrate_test.yml
 
uuid: 5c12fdab-6767-485e-933c-fd17ed554b49
status: true
dependencies:
  enforced:
    module:
      - custom_migrate
id: migrate_test
label: migration test
migration_tags:
  - CSV
source:
  plugin: csv
  path: private://artifacts/test.csv
  delimiter: ','
  enclosure: '"'
  header_row_count: 1
  keys:
    - id
  column_names:
    # this id must be unique, it is used for rollback or update.
    0:
      id: 'ID'
    1:
      title: 'Title'
    2:
      body: 'Body' 
    # taxonomy “category”
    3:
      category: 'Category', example : cat1 (can use with explode plugin here, but I didn’t test it)
    # images field “unlimited”
    4:
      images: 'Images',;as separator, example : filename.png;filename2.png
    # text field “unlimited”, 
    5:
      group: 'Group'  
    # date field (Date only), example : 22-02-2017
    6:
      date: 'Date'
    # paragraph id, “+” as separator, example : 1+3+4+5+6+7
    7:
      paragraph: 'Paragraph'  
process:
  type:
    plugin: default_value
    default_value: test
  title : title
  body/value: body
  body/format:
    plugin: default_value
    default_value: full_html
  # generate category if not exist
  field_category:
    plugin: entity_generate
    source: category
    value_key: name
    bundle_key: vid
    bundle: category
    entity_type: taxonomy_term
  # this will import multiples images per cell, here the separator is “;”
  field_image:
    -
      plugin: explode
      source: images
      delimiter: ;
    -
      plugin: callback
      callable: trim
    -
      plugin: migration
      migration: migrate_images
      no_stub: true
  # import the “alt” value for each image
  # field_image:
  #  -
  #    plugin: explode
  #    source: alt
  #    delimiter: ;
  #  -
  #    plugin: get
  #    source: alt
  # import multiple values here
  field_group:
    -
      plugin: explode
      delimiter: ,
      source: group
  field_date/value:
    -
      plugin: skip_on_empty
      method: process
      source: date
    -
      plugin: format_date
      from_format: 'd-m-Y'
      to_format: 'Y-m-d'
      source: date
  # import link field (link format)
  field_link: link
  # imoprt multiple paragraphs
  field_paragraph:
    -
      plugin: skip_on_empty
      method: process
      source: paragraph
    -
      plugin: explode
      delimiter: +
      source: paragraph
    -
      plugin: key_wrapper
    -
      plugin: iterator
      process:
        target_id:
          -
            plugin: migration
            migration: migrate_paragraph
            source: key_wrapper
            no_stub: true
          -
            plugin: extract
            index:
              - 0
        target_revision_id:
          -
            plugin: migration
            migration: migrate_paragraph
            source: key_wrapper
            no_stub: true
          -
            plugin: extract
            index:
              - 1
  created:
    plugin: callback
    source: created
    callable: strtotime
    created: date
  changed: '@created'
  uid:
    -
      plugin: entity_lookup
      source: created_by
      ignore_case: true
      value_key: mail
      entity_type: user
      bundle_key: status
      bundle: 1
    -
      plugin: default_value
      default_value: 1
  status:
    plugin: default_value
    default_value: 1
    source: status
destination:
  plugin: entity:node
migration_dependencies:
  required:
    - migrate_images
    - migrate_paragraphs
  optional: {}
 
 
--  /modules/custom/custom_migrate/config/install/migrate_plus.migration.migrate_images.yml
 
uuid: 5c12fdab-6767-485e-933c-fd17ed554b99
dependencies:
  module:
    - file
id: migrate_images
migration_tags:
  - CSV
label: Images
source:
  delimiter: ','
  enclosure: '"'
  constants:
    source_base_path: 'private://assets/images'
    uri_file: 'public://images'
  plugin: csv
  track_changes: true
  path: private://artifacts/images.csv
  header_row_count: 1
  keys:
    - name
  column_names:
    1:
      name: Images
process:
  source_full_path:
    -
      plugin: concat
      delimiter: /
      source:
        - constants/source_base_path
        - name
    -
      plugin: urlencode
  uri_file:
    -
      plugin: concat
      delimiter: /
      source:
        - constants/uri_file
        - name
    -
      plugin: urlencode
  filename:
    -
      plugin: skip_on_empty
      method: row
      source: name
  uri:
    plugin: file_copy
    source:
      - '@source_full_path'
      - '@uri_file'
destination:
  plugin: 'entity:file'
migration_dependencies:
  required: { }
  optional: { }
 
 
 
/modules/custom/custom_migrate/config/install/migrate_plus.migration.migrate_paragraph.yml
 
uuid: 5c12fdab-6767-485e-933c-fd17ed234567
dependencies:
  module:
id: migrate_paragraph
migration_tags:
  - CSV
label: "Paragraph"
source:
  delimiter: ','
  enclosure: '"'
  plugin: csv
  path: private://artifacts/paragraph.csv
  header_row_count: 1
  keys:
    - id
  column_names:
    0:
      id: "id"
    # just a text field here
    1:
      test: "test"
process:
  field_test: test
  type:
    plugin: default_value
    default_value: paragraph
destination:
  plugin: 'entity_reference_revisions:paragraph'
migration_dependencies:
  required: {  }
  optional: {  }
Help Share this Article