Score:1

Import content in nodes with media:image fields using Migrate from csv source

mx flag

I try to import content from a .csv file into article nodes of a fresh D9 install using Migrate, Migrate Plus, Migrate Tools, Migrate csv source and Migrate File (extended)

My article content type has a field_article_media_image expecting a media image item.

When I run the migration, the articles are created correctly BUT the field_article_media_image is still empty. I see the image has been imported in the filesystem, but created as a file, not as a media item (not present in the media tab) and it is not linked to the article.

//import-articles.yml

id: import_articles
label: Import articles
migration_group: import_articles_group

destination:
  plugin: entity:node

source:
  constants:
    DESTINATION_PATH: 'public://migrations/articles/images/'
  plugin: 'csv'
  path: 'migrations/articles/articles.csv'
  delimiter: ';'
  enclosure: '"'
  header_offset: 0
  ids:
    - id
  fields:
    0:
      name: id
      label: 'Id'
    1:
      name: title
      label: 'Title'
    2:
      name: body
      label: 'Body'
    3:
      name: field_article_media_image
      label: 'Image'
process:
  title: title
  body/value: body
  body/format:
    plugin: default_value
    default_value: 'basic_html'
  field_article_media_image:
    - plugin: image_import
      source: field_article_media_image
      destination: constants/DESTINATION_PATH
      title: title
      skip_on_error: true
    - plugin: log
  type:
    plugin: default_value
    default_value: article

The log gives no error. Am I missing something or am I doing something wrong?

EDIT

After what @Joseph commented, I tried this code:

...
    field_article_media_image:
        source: field_article_media_image
        plugin: entity_lookup
        entity_type: media
        bundle: image
        bundle_key: bundle
        value_key: 'name'
        access_check: 0
...

Everything is working now, I think value_key needed quotes, and maybe access_check had to be set to false.

Score:0
in flag

How your article is related to the image is actually like this:

Article (node) -> Image (media) -> File (file)

So there are actually three migrations you have to do, assuming you're not doing anything special and just doing vanilla migrations.

  • The first migration will be to a File entity. The destination of this migration is entity:file which will migrate the information to the file_managed table. This is what migrates file metadata, like where it is in the file system, when it was changed, when it was modified, etc.

  • The second migration will be to a Media entity. The destination of this migration is entity:media with bundle image, which will create a media entity of the file. This migration is usually 1:1 with the file it's representing. This is the entity that should be related to by other entities in the system. You do a migrate_lookup on the File migration using that file's ID.

    • I believe this is the migration you're missing.
  • The last migration will be your Article migration, which is your article data from your CSV (the migration you wrote). You'll need to do a migrate_lookup on the media migration using the file's ID for field_article_media_image.

mx flag
In the meantime, I managed to import the images as Media entities (using drupal/media_library_importer). Can I go directly to the last migration? The title for the Media item is a unique key I've got in my .csv
in flag
@laurent If you used tools other than Migrate API to migrate Media, your article migration cannot use the [`migration_lookup`](https://api.drupal.org/api/drupal/core!modules!migrate!src!Plugin!migrate!process!MigrationLookup.php/class/MigrationLookup) plugin to link article to media. But you can create a [process plugin](https://www.drupal.org/docs/8/api/migrate-api/migrate-process/writing-a-process-plugin) and use that column in your CSV to find the correct media entity in the DB using [Entity API](https://www.drupal.org/docs/drupal-apis/entity-api/introduction-to-entity-api-in-drupal-8).
mx flag
In Migrate Plus, the plugin `entity_lookup` seems to be doing just what I want, the example provided is _hooking_ nodes to users entities, I don't know how to transpose to media entities, but that does not sound overly complicated . I'm new to Drupal, so if I can avoid writing a plugin...
in flag
@laurent `entity_lookup` can do what I described. You just need to configure it to point to the right `entity_type` ("media"), `bundle` ("image"), `bundle_key` ("bundle"), and most importantly `value_key` - the machine name of the field in your media entity that holds the value that coincides with the value in your articles CSV.
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.