There is currently no way to migrate field_collection_items into a D8 site because no MigrateDestinationInterface class exists to set the host entity for an incoming field_collection_item entity.

Comments

caxy4 created an issue. See original summary.

caxy4’s picture

The patch below implements a MigrateDestinationInterface class to set the host entity of an incoming field_collection_item.
This allows basic migrations of field_collection_items to work provided two required destination properties exist:

  • host_type
  • host_entity_id

I chose the name of the host_type destination property to be in-line with the field_collection_item table schema.

jmuzz’s picture

Thanks @caxy4.

I'm interested in this approach of importing the field collection item entities and letting the fields in the host get set by saving the field collection item. It seems like an effective way to bypass the problems with migrating the fields directly.

I tried applying the patch and clearing my cache. I'm not seeing anything different at /upgrade on my test site though.

I'd like to delve into this more deeply. Maybe you can explain what I am missing? I expected field_collection_item to show there along with node etc.

I also tried running the migration but the data doesn't seem to transfer over correctly.

jmuzz’s picture

caxy4’s picture

Uploading a slightly modified patch that I meant to upload initially.

Two differences: name of host_type destination property changed from "host_entity_type" to "host_type" and returning an array instead of an int.

caxy4’s picture

@jmuzz with respect to your question about not seeing anything different at /upgrade:

I wouldn't expect you'd see anything different. This patch only exposes a migration destination plugin a migration could make use of; I haven't added a migrate path from D7.

The use case I developed the approach to solve is migrating CSV data, which, with the help of migrate_tools, migrate_plus, and migrate_source_csv, can be done by registering a migration with a YAML file (see Using the Migrate Source CSV plugin).

The only constraints imposed by my patch is that your YAML file must define "host_type" and "host_entity_id" via it's process plugin and use the destination plugin the patch defines:

process:
  host_type:
    plugin: default_value
    default_value: redhen_org
  host_entity_id:
    plugin: migration
    migration: students
    source: host_entity_id
destination:
  plugin: 'entity:field_collection_item'

  • jmuzz committed 5bed0aa on 8.x-1.x authored by caxy4
    Issue #2721823 by caxy4: Add migrate destination plugin so...

  • jmuzz committed cce8ad8 on 8.x-1.x authored by caxy4
    Issue #2721823 by caxy4: Add migrate destination plugin so...
jmuzz’s picture

Status: Needs review » Fixed

Thanks @caxy4.

I wonder if there is a plugin in core it could be tested with.

If not, tests will need to be added when there is a way to migrate field collections from Drupal 7.

killes@www.drop.org’s picture

Thanks for providing this!

I am trying to use this using this setup:

process:
  host_type:
    plugin: default_value
    default_value: repository
  host_entity_id:
    plugin: migration
    migration: repository
    source: handle
  # assigning values to the fields of the field collection
  field_url: url
  field_type: urltype

however I get

Missing bundle for entity type field_collection_item

Do I need to set field_name somewhere?

killes@www.drop.org’s picture

This worked for me:

process:
# the name of the field collection bundle
  field_name:
    plugin: default_value
    default_value: field_urls
# this is the host type, you need to specify the entity type, ie "node" in most cases.
  host_type:
    plugin: default_value
    default_value: node
# get the matching entity IDs from a previous migration named "repository" using the "handle" parameter from our input.
  host_entity_id:
    plugin: migration
    migration: repository
    source: handle
 # fields of the field_collection entity:
  field_url: url
  field_type: urltype
killes@www.drop.org’s picture

Hmm, when I try to update I get:

throw new \Exception(t('The host entity may be set only during creation of a field collection item.'));

this is from

public function setHostEntity($entity, $create_link = TRUE) {

in

field_collection/src/Entity/FieldCollectionItem.php

Not sure why this is, the host entity setting could be ignored instead.

tjferre’s picture

getting the following error when using drupal /upgrade,

Attempt to create a field field_xyz that does not exist on entity type field_collection_item.

Any ideas?

jmuzz’s picture

If any of these questions are about upgrading from a previous version of Drupal, that part isn't done yet, but there is information about it here: #2715397: Migration path from Drupal 7.x

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.