Migrating Drupal File Fields to Media Entities without the Migrate Module

With the advent of the media initiative, Drupal now has a clear best practice way of handling images, audio, and other forms of media. Many sites (including ours) were previously using simple file fields to store images. To update our content model a migration was in order. However a traditional migration using the Migrate API seemed like overkill for the scope of our content. This is likely the case for many other small sites, so let’s walk through our solution step by step.

Create Media Entities

The first step is to create the new media entities. For this example we will be using a media bundle named image. A file field named field_media_image should be added to this new entity.

Create Media Reference Fields

Next we need to create entity reference fields to replace our existing file fields on all of our content types. Leave the file fields in place for now, as we will use them as the source of our migration.

Migrate Image Files

With our limited content, we were able to do this process in an update hook without the aid of the batch process. This same process could be easily adapted for batch integration if needed though.

/**
* Create media entities from existing file fields.
*/

function example_update_8401() {
// Load all of the article and page nodes.
$nodes = \Drupal::entityTypeManager()
->getStorage('node')
->loadByProperties(['type' => ['article', 'page']]);
foreach ($nodes as $node) {
// Verify that the file field has a value.
if (!empty($node->field_file_image->entity)) {
// Create the new media entity and assign it to the new field.
$node->field_media_image->entity = example_create_media_image_entity(
$node->field_file_image->entity,
$node->field_file_image->alt
);
$node->save();
\Drupal::logger('chromatic_image')->notice(
sprintf('Updated image for node "%s".', $node->getTitle())
);
}
}
}

To aid in the creation of our media image entity, we created a helper function to encapsulate its creation.

use Drupal\media_entity\Entity\Media;

/**
* Creates a media image entity from a file entity.
*
* @param \Drupal\file\FileInterface $file
* The existing file object.
* @param string $alt
* The image alt text.
*
* @return \Drupal\media_entity\Entity\Media
* The media entity.
*/

function example_create_media_image_entity(FileInterface $file, $alt = NULL) {
$media_entity = Media::create([
'bundle' => 'image',
'uid' => '1',
'name' => $file->alt,
'status' => Media::PUBLISHED,
'field_media_image' => [
'target_id' => $file->id(),
'alt' => $alt,
],
]);
$media_entity->save();
return $media_entity;
}

Cleanup

With the new field in place and the data successfully migrated, the file field can now be deleted from our nodes. This can be done via the admin UI or with another update hook depending on your site’s workflow.

Shiny New Toy

With the new media entity content model, additional data can be easily associated with each image by simply adding fields to the media entity. Now functionality such as caption text, copyright information, or alternate crop data can be handled with ease!