migrate_file_to_media
This module allows you to migrate file entities to media entities using the migrate module.
Lately, also support for Drupal 7 entities was added.
Main features:
- While migrating the files, a binary hash of all images is calculated and duplicate files are recognized. If the same file was uploaded multiple times, only one media entity will be created.
- Migration of translated file/image fields is supported. Having different images per language will create a translated media entity with the corresponding image.
- Using migrate module allows drush processing, rollback and track changes.
Usage
- Install the module.
- If you want to migrate file fields for all content revisions then you have to fix the issues related to the content moderate and revisions. For this, you can use known core patches:
Preparation: Install media core module
Before you can start, you need to install the media module of Drupal Core. This will automatically create 5 media bundle types for you: Image, Document, Video, Remote video and Audio. Media Library is a handy module here.
Generate the target media fields
Generate the media fields based on the existing file fields using the following drush command:
drush migrate:file-media-fields <entity_type> <bundle> <source_field_type> <target_media_bundle>
Example
drush migrate:file-media-fields node article image imageFor all file fields the corresponding media entity reference fields will be automatically created suffixed by _media.
Create a custom migration per content type
- Create a custom module. It could be based on the migrate_file_to_media_example module (optional)
- Create your custom migration templates using the included yml generator.
- The module supports nodes and also paragraphs and any other entity type as a source bundle.
Generate command:
drush generate mf2m_mediaExample of the result:
Welcome to mf2m_media generator! ––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––– Module machine name: ➤ migrate_file_to_media_example
The generator will ask you several questions about the module, the source bundle and the target media entity. It will generate several files based on your answers, that can be used to migrate the files to media entity.
Explanation of the generated migration files is in the README.md
Refresh / check migrations
If you created a new module with the generator, you can just enable the module and the migrations will be visible when running drush ms. If you generated the migration files into an existing module, you will have to install the module config_devel and update your mymodule.info.yml file. You can find a nice documentation about config_devel here: https://drupal.stackexchange.com/questions/191435/how-to-refresh-new-mig...
Prepare duplicate file detection
In order to detect duplicate files / images, run the following drush command to calculate a binary hash for all files. The data will be saved to the table "migrate_file_to_media_mapping". You need to run this drush command before you can import media entities.
drush migrate:duplicate-file-detection <migration_name>(optional step): Check for existing medias
Sometimes you already have media entities in your database or you have multiple migrations and would like to avoid duplicate media entities. Here is the solution:
- Run the command
drush migrate:duplicate-media-detection image --all. (seedrush help migrate:duplicate-media-detectionfor extra options) - Run the duplicate file detection for step 1 of your file migration:
drush migrate:duplicate-file-detection <migration_name> --check-existing-media - Run step 1 and 2 of your file migrations as described on the project page or the readme.
Migrate the images to media entities
Check the migrations using
drush migrate:statusRun the migrations using
drush migrate:import <migration_name>