Skip to content

Instantly share code, notes, and snippets.

@onesixromcom
Created March 12, 2021 22:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save onesixromcom/d8be7e86c25821aea30a6fb5f380ddf2 to your computer and use it in GitHub Desktop.
Save onesixromcom/d8be7e86c25821aea30a6fb5f380ddf2 to your computer and use it in GitHub Desktop.
Create media entities from existing file fields. Drupal 7 to 8 (9) migration.
<?php
use Drupal\media\Entity\Media;
use Drupal\file\Entity\File;
/**
* Create media entities from existing file fields.
*/
function mymodule_update_8801() {
$converts = [
[
'content_type' => ['article', 'game'],
'media_bundle' => 'image_thumb',
'new_node_image_field' => 'field_image_thumb',
'old_node_image_field' => 'field_image'
],
[
'content_type' => ['humor'],
'media_bundle' => 'image_humor',
'new_node_image_field' => 'field_image_humor',
'old_node_image_field' => 'field_image'
],
];
foreach ($converts as $convert) {
$old_field = $convert['old_node_image_field'];
// Load all nodes of current CT.
$nodes = \Drupal::entityTypeManager()
->getStorage('node')
->loadByProperties([
'type' => $convert['content_type']]
);
$media_ids_remove = [];
foreach ($nodes as $node) {
// Verify that the file field has a value.
if (!empty($node->field_image->entity)) {
$medias_thumb = \Drupal::entityTypeManager()
->getStorage('media')
->loadByProperties([
'bundle' => [$convert['media_bundle']],
'name' => $node->{$old_field}->entity->label()]
);
if (!empty($medias_thumb)) {
$new_field = $convert['new_node_image_field'];
$node->{$new_field}->entity = reset($medias_thumb);
$node->save();
\Drupal::logger('image_to_media')->notice(
sprintf('Reuse IMAGE_THUMB for node "%s".', $node->getTitle())
);
}
else {
$medias = \Drupal::entityTypeManager()
->getStorage('media')
->loadByProperties([
'bundle' => ['image'],
'name' => $node->{$old_field}->entity->label()]
);
if (!empty($medias)) {
$media = reset($medias);
$file_id = $media->field_media_image->target_id;
$file = File::load($file_id);
$new_field = $convert['new_node_image_field'];
$node->{$new_field}->entity = _mymodule_create_media_image_entity(
$convert['media_bundle'],
$file,
$node->{$old_field}->alt
);
$node->save();
$media_ids_remove[] = $media->id();
\Drupal::logger('image_to_media')->notice(
sprintf('Reuse media for node "%s".', $node->getTitle())
);
}
else {
\Drupal::logger('image_to_media')->notice(
sprintf('NO IMAGE "%s".', $node->getTitle())
);
}
}
}
}
// Old images will be deleted, to prevent duplicates.
if (!empty($media_ids_remove)) {
$storage_handler = \Drupal::entityTypeManager()
->getStorage('media');
$entities = $storage_handler
->loadMultiple($media_ids_remove);
$storage_handler
->delete($entities);
}
}
}
/**
* Creates a media image entity from a file entity.
*
* @param $media_bundle
* @param \Drupal\file\Entity\File $file
* The existing file object.
* @param null $alt
* The image alt text.
*
* @return \Drupal\Core\Entity\EntityBase|\Drupal\Core\Entity\EntityInterface|\Drupal\media_entity\Entity\Media
* The media entity.
* @throws \Drupal\Core\Entity\EntityStorageException
*/
function _mymodule_create_media_image_entity($media_bundle, File $file, $alt = NULL) {
$media_entity = Media::create([
'bundle' => $media_bundle,
'uid' => '1',
'langcode' => \Drupal::languageManager()->getDefaultLanguage()->getId(),
'name' => $file->label(),
'field_media_image' => [
'target_id' => $file->id(),
'alt' => $alt,
],
]);
$media_entity->save();
return $media_entity;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment