This issue relates specifically to the patch here: #1175082: Field collection field handler for migrate module.

I created this new thread because I didn't want to flood the other one with support requests seeing as the patch apparently works and the issue is already RTBC.

@mikeryan, the code comments at the top of field_collection.migrate.inc state that the Field Collection migration must introduce a dependency on the host entity migration and link them like this:

$this->addFieldMapping('host_entity_id', 'source_article_id')->sourceMigration('Article');

My question is related to the "source_article_id" part; where do we obtain this? Assuming a migration from CSV where each article (to stick with the example) doesn't necessarily have an id? Should we create a dummy source field for use with the migration?

Thanks!

Comments

mikeryan’s picture

Title: Question related to FieldCollection Migrate Destination » Source ID for host_entity_id in migration
Status: Active » Postponed (maintainer needs more info)

The "source_article_id" would be the same source ID that was used in the Article migration. So, if your source article data looks like:

ArticleID Title Body
5 Article 1 some stuff...
8 Article 2 some more stuff...
...

And your collections are in a related table:

ArticleID CollectionDelta CField1 CField2
5 0 First field value 23
5 1 Second field value 58
8 0 Another value -238

Your ArticleMigration class would include

    $this->map = new MigrateSQLMap($this->machineName,
      array(
        'ArticleID' => array('type' => 'int',
                       'unsigned' => TRUE,
                       'not null' => TRUE,
                       'description' => 'Source article ID',
                      ),
      ),
      MigrateDestinationNode::getKeySchema()
    );

And your collection migration class would map the source ID of the original collection's parent to host_entity_id, with the sourceMigration() translating the original source ID to the Drupal nid created from that article.

$this->addFieldMapping('host_entity_id', 'ArticleID')
     ->sourceMigration('Article');

So, can you explain how it is that there might not be an article ID in your source data? There must be some way of relating the collection data to its parent entity - you must pass in the ID of the parent entity to the collection migration to be able to relate it properly.

Edit: Fixed table HTML

alexweber’s picture

Status: Postponed (maintainer needs more info) » Fixed

Thank you very much for the detailed info!

To clarify for others: I'm importing content from a CSV file so there was no ID field in the export so I was using the title as the key for the mapping.

It works fine but I ran into issues when I needed to map the host entity to the field collection.

I just added a dummy id to the article export as per Mike's example and the field collection now maps properly.

Pls’s picture

#1 is really nice explanation why Source Migration is needed for field collection items migration. Thanks for that information, actually it should be somewhere as documentation.

However, if my so called "Articles" nodes are already existing in Drupal SQL table and I only need to migrate "Article" content type imagefield multiple values to field collection items attached to this "Article" type.

The problem is that if I will do "Article" migration first, then I will have duplicate nodes with field collection items attached to them. Then would have to delete the old nodes to get the end result.

What is the best solution to my problem? Maby I'm misunderstanding recommended approach?

I'm really trying to figure out the best method here, so thanks for any help.

mikeryan’s picture

@Pls - you just need to map the nid of the node you want to parent your collection fields to host_entity_id, you don't need sourceMigration.

Status: Fixed » Closed (fixed)

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