Migrate files to media

Migrate files to media

Is usual for the drupal development to face a file migration to media entities for their reuse. As a first solution we can take this migration through the migrate module of drupal in which is necessary the implementation of logic to get it. But why should we work from zero when you can use the already existing module given by Migrate File Entities to Media Entities which makes this implementation easier?

In this post we will see how to make a file migration to media entities using the migrate file entities to media entities module.

Main module features:

  • Detection of duplicate files.
  • Migration of files translations.
  • Use of drush commands to manage the migration.

Steps for making a migration:

Installation of the module

Is necessary the installation of Migrate File Entities To Media entities module. For this we need to run in our console the command:

composer require drupal/migrate_file_to_media

After Composer downloads the module with its dependencies we have to enable the module in our drupal system.

drush en migrate_file_to_media

Creation of destination media fields.

All data migration always has an origin and a destination. For this module the origin is the field file type which will be the reference in the migration and the destination is the field media that we will have to create for the migration.

This module already give us a drush command “migrate:file-media-fields” for the creation of these fields, which means that it is not necessary to create them in drupal. For this we need to run the next command:

drush migrate:file-media-fields <entity_type> <bundle> <source_field_type> <target_media_bundle>
  • <entity_type>: Type of entity, it can be node, paragraphs or any other type of bundle.
  • <bundle>: Type of bundle or mechanism name of the entity.
  • <source_field_type>: Type of origin fields to migrate (image, file, etc).
  • <target_media_bundle>: Type of bundle media entities of the destination field.  (image, video, file o audio).

This command automatically acknowledges the existing fields (source_field_type) inside a type of content and creates its destination media field there. The name of the destination field will be the same as the origin with the suffix _media.


If we need to migrate an image field to a media entity with the type image on the type of content article:

drush migrate:file-media-fields node article image image

The result of this command will be the creation of a new type of field in which the content type is the article.

Creation of a custom module.

For the creation of a migration we need to make a custom module at has as dependency with the module migrate_file_to_media. Once we have that done we can start to create the ymls of configuration for the incoming migrations.

Creation of the configuration ymls.

There are two options for the creation of the configuration ymls:

Drush command:

From the console we run this:

drush generate yml-migrate_file_to_media_migration_media

After running this command we will be asked for a variant series of openings for the creation of the ymls inside our module.

Module machine name:
 ➤ basic_migration_example

 Module name:
 ➤ Basic Migration Example 101

 Plugin label [Example]:
 ➤ article_media_basic_migration

 Plugin ID [example]:
 ➤ article_media_basic_migration

 Migration Group [media]:
 ➤ ( Press Enter Button to use suggested "media" )

 Entity Type [node]:
 ➤ ( Press Enter Button to use suggested "node")

 Source Bundle:
 ➤ article

 Source Field Names (comma separated) [field_image]:
 ➤ ( Press Enter Button to use suggested "field_image" )

 Target Media Type [image]:
 ➤ ( Press Enter Button to use suggested "image" )

 Target Field [field_media_image]:
 ➤ ( Press Enter Button to use suggested "field_media_image" )

 Language Code [en]:
 ➤ ( Press Enter Button to use suggested "en" )

 Translation languages (comma separated) [none]:
 ➤ ( Press Enter Button "none" )

Once we have filled all the steps it will create inside our module the ymls of configuration we need for the migration.

Create yml files manually:

Inside the Migrate File Entities To Media Entities we can find a submodule called Migrate File To Media Example which inside the directory config/install has examples of ymls for the realization of the migration. These files are:

  • migrate_plus.migration.migrate_file_to_media_example_article_images_step1.yml: Which creates a single multimedia entity on base of the references in the opening fields of configuration “field_names” del source plugin. This will be the migration that the subsequent ones will depend on.
  • migrate_plus.migration.migrate_file_to_media_example_article_images_step1_de.yml
    If we need to migrate the translation of the files is necessary to create a yml file like this one in which we add the list of fields to migrate (field_names) and we use the yml as a dependency for the translation.
  • migrate_plus.migration.migrate_file_to_media_example_article_images_step2.yml
    This yml is the one in charge of recovering a type of content in the origin files to migrate and locate them in their destination.

Notes: When is necessary to realize a migration with translation it is necessary to add in the yml of the step 2 the attribute “include_translation:true” inside of the source plugin.

Executing the migrations:

Migrations status.

With the next command we can see the status of the migration:

drush migrate:status

If you need more information about this commands you can check the next link Content Migration - Managing Migrations (Migrate tools)

Prepare the detection of duplicate files.

For detecting duplicate files we simply run the next command which calculates the binary hash for every file inside the chart migrate_file_to_media_mapping.

drush migrate:duplicate-file-detection <migration_name>

In case that we have already created media files in our project or that we have implemented several migrations it will be necessary to detect the duplicated multimedia entities.

For this we run the command:

drush migrate:duplicate-media-detection image --all

Starting the migration.

With the next command we start the importation of a migration for the reference group:

drush migrate:import --group=media

If you need more information about this commands you can check the next link Content Migration - Managing Migrations (Migrate tools).

In Conclusion.

The module “Migrate File Entities to Media Entities” makes the migration of files to media entities a lot easier.