Created
March 30, 2020 23:10
-
-
Save laradevitt/8a911ffa00d58afe17dc1ad5796b3a45 to your computer and use it in GitHub Desktop.
(Drupal 8) Convert file entities to media entities with batch update script
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
use Drupal\media\Entity\Media; | |
/** | |
* @file | |
*/ | |
/** | |
* Convert transient file entities that weren't processed during migration. | |
*/ | |
function convert_file_to_media_update_8101(&$sandbox) { | |
$mime_types = [ | |
'image/jpeg', | |
'image/png', | |
'image/gif', | |
'application/pdf', | |
]; | |
if (!isset($sandbox['progress'])) { | |
$fids = \Drupal::entityQuery('file') | |
->condition('uri', 'public://%', 'LIKE') | |
->condition('filemime', $mime_types, 'IN') | |
->execute(); | |
$sandbox['progress'] = 0; | |
$sandbox['total'] = count($fids); | |
$sandbox['created'] = 0; | |
$sandbox['current'] = 0; | |
} | |
$entities_per_batch = 25; | |
$fids = \Drupal::entityQuery('file') | |
->condition('uri', 'public://%', 'LIKE') | |
->condition('filemime', $mime_types, 'IN') | |
->range($sandbox['current'], $sandbox['current'] + $entities_per_batch) | |
->execute(); | |
foreach ($fids as $id) { | |
$file = \Drupal\file\Entity\File::load($id); | |
$fid = $file->id(); | |
$file_created = $file->getCreatedTime(); | |
$file_changed = $file->getChangedTime(); | |
$filename = $file->getFilename(); | |
$mimetype = $file->getMimeType(); | |
$bundles = ['image', 'file']; | |
$query = \Drupal::entityQuery('media') | |
->condition('bundle', $bundles, 'IN'); | |
$orGroup = $query->orConditionGroup() | |
->condition('field_media_image', $fid) | |
->condition('field_media_file', $fid); | |
$entity_ids = $query->condition($orGroup)->execute(); | |
if (empty($entity_ids)) { | |
switch ($mimetype) { | |
case 'image/jpeg': | |
case 'image/png': | |
case 'image/gif': | |
// Create 'Image' media entity. | |
$media = Media::create([ | |
'bundle' => 'image', | |
'uid' => 1, | |
'name' => $filename, | |
'field_media_image' => $file, | |
]); | |
$media->setCreatedTime($file_created); | |
$media->setChangedTime($file_changed); | |
$media->save(); | |
$sandbox['created']++; | |
break; | |
case 'application/pdf': | |
// Create 'Document' media entity. | |
$media = Media::create([ | |
'bundle' => 'file', | |
'uid' => 1, | |
'name' => $filename, | |
'field_media_title' => $filename, | |
'field_media_file' => $file, | |
]); | |
$media->setCreatedTime($file_created); | |
$media->setChangedTime($file_changed); | |
$media->save(); | |
$sandbox['created']++; | |
break; | |
} | |
} | |
$sandbox['current']++; | |
} | |
\Drupal::logger('convert_file_to_media')->notice($sandbox['current'] . ' file entities processed. ' . $sandbox['created'] . ' media entities created.'); | |
if ($sandbox['total'] == 0) { | |
$sandbox['#finished'] = 1; | |
} else { | |
$sandbox['#finished'] = ($sandbox['current'] / $sandbox['total']); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment