Skip to main content
Drupal is a registered trademark of Dries Buytaert
Release: OpenID Connect / OAuth client 3.0.0-alpha9 New alpha version released for module openid_connect (3.0.0-alpha9). Usage Milestone: Google Analytics Module google_analytics crossed 1,000 active installs. Release: Timelinr 1.0.1 Minor update available for module timelinr (1.0.1). Release: GraphQL Compose Codegen 1.1.2 Minor update available for module graphql_compose_codegen (1.1.2). Release: Mapy.com 1.1.3 Minor update available for module mapycom (1.1.3). Release: Ckeditor5 entity browser 3.0.3 Minor update available for module ckeditor5_entity_browser (3.0.3). Release: Ckeditor5 entity browser 3.0.1 Minor update available for module ckeditor5_entity_browser (3.0.1). Release: Ckeditor5 entity browser 3.0.2 Minor update available for module ckeditor5_entity_browser (3.0.2). Release: Teamleader Integration 4.0.2 Minor update available for module teamleader (4.0.2). Module Revived: Entityqueue Buttons 1.1.2 Module entityqueue_buttons updated after 8 months of inactivity (1.1.2).

Migrate File Entities to Media Entities

1,654 sites Security covered Drupal 10–11
View on drupal.org

This module helps migrate file entities to media entities, supporting duplicate file detection and translated fields. It also offers Drupal 7 entity migration and integrates with the core migrate module for advanced processing.

Migrate File Entities to Media Entities

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

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 image

For 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_media

Example 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:

  1. Run the command drush migrate:duplicate-media-detection image --all. (see drush help migrate:duplicate-media-detection for extra options)
  2. Run the duplicate file detection for step 1 of your file migration: drush migrate:duplicate-file-detection <migration_name> --check-existing-media
  3. 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:status

Run the migrations using

drush migrate:import <migration_name>

Depends on

Dependencies of the latest stable release

Required by

Tracked projects that depend on this one

No tracked projects depend on this one yet.

Activity

Tracked releases
3
Tracked since
Sep 2024
Latest release
6 months ago
Releases (12 mo)
1 ▼ from 2
Maintenance
Active

Release Timeline

Releases

Version Type Core Release date
2.0.15 Stable 10–11 Feb 13, 2026
2.0.14 Stable 10–11 Nov 17, 2024
2.0.13 Stable 9–10 Sep 27, 2024