File Entity to Media Migration in Drupal: Observations.

Posted on 17 Jun 2021 by Alexei Raiu
File Entity to Media Migration in Drupal: Observations.

In the middle of its life cycle, Drupal 8 introduced in core Media, and for about a year, continued to add functionality until it became usable. Media has a library, allows reusability, and allows adding custom fields to the media entities. Some of the websites I worked with were built with the file_entity module that made the files fieldable, and entity_browser module, which provided a UI library for reusing files. That module combination is incompatible with Media and needed to be dismantled, but not before the functionality got migrated to Media. Here are some thoughts on this process.

Files by themselves don't have types, or "bundles", as Drupal calls it. Files differ in MIME type, but architecture-wise, all files are the same. You can't add fields to files. Copyright? Comment? Caption? Forget it. The file_entity approach comes from Drupal 7. The file_entity module "upgraded" the file entity, made it fieldable, added types support to it. Without the Media in core, this approach became very popular.

The Media was introduced in core in Drupal 8.7 - which is somewhere in the middle of its life cycle. It was not until Drupal 8.9 or even 8.10 that Media started being usable. Most of the sites were built with file_entity instead, and now needed to change their media architecture.

Media has a different approach than the file_entity, and is incompatible with it. With Media, the file still remains the file as it was before - no fields, no types. However, files can be embedded in a Media entity. And it's Media entity types that can have fields, types, and be used in it's core Media Library. The file entities remain there, but they are now masked by Media, and you deal with Media in your editor experience now.

It is a common misconception to think that the files are getting superseded by the Media. No, the files remain - they just turn back from a princess to a cinderella when the file_entity is removed, and Media now masks them.

Thus, you need to migrate rather than upgrade. The best approach here is to duplicate the architecture with Media in mind, then migrate the files into Media, attach the Media to the new duplicated fields, migrate custom code, and then check everything 7 times before removing the old image fields and all the configuration.

From my experience, if you have lots of file_entity fields, and a strongly-developed code infrastructure to work with those file_entity fields, then it will take time and effort to identify and convert that functionality to work with Media. In most cases, because the architecture has increased complexity (it's a matryoshka doll now), you will need to do more work than simply change entity names and variables.

Tell me if you have questions or whether you would like me to talk more about some aspects of the file to Media migration in Drupal.