function photos_post_update_migrate_photos_image_entity_1 in Album Photos 8.5
Same name and namespace in other branches
- 6.0.x photos.post_update.php \photos_post_update_migrate_photos_image_entity_1()
Migrate images to new photos_image entity.
File
- ./
photos.post_update.php, line 15 - Post update functions for Photos module.
Code
function photos_post_update_migrate_photos_image_entity_1(&$sandbox = NULL) {
// Check if temporary table was created.
if (\Drupal::database()
->schema()
->tableExists('photos_image_tmp')) {
if (!isset($sandbox['steps'])) {
$sandbox['limit'] = 25;
$sandbox['current_step'] = 0;
$sandbox['current_fid'] = 0;
// Count query.
$query = \Drupal::database()
->select('photos_image_tmp', 'i')
->fields('i', [
'fid',
]);
$num_rows = $query
->countQuery()
->execute()
->fetchField();
$sandbox['steps'] = $num_rows;
}
$results = \Drupal::database()
->select('photos_image_tmp', 'i')
->fields('i')
->condition('i.fid', $sandbox['current_fid'], '>')
->range(0, $sandbox['limit'])
->orderBy('i.fid', 'ASC')
->execute();
foreach ($results as $result) {
$fid = $result->fid;
$sandbox['current_step']++;
$sandbox['current_fid'] = $fid;
try {
$title = $result->title;
/* @var \Drupal\file\Entity\File $file */
$file = \Drupal::entityTypeManager()
->getStorage('file')
->load($fid);
$image = \Drupal::service('image.factory')
->get($file
->getFileUri());
if ($image) {
if (empty($title)) {
// @note known issue: title can not be null.
$title = \Drupal::service('photos.upload')
->cleanTitle($file
->getFilename());
}
try {
// Create new photos_image entity.
$photosImage = \Drupal::entityTypeManager()
->getStorage('photos_image')
->create([
'album_id' => $result->pid,
'title' => $title,
'weight' => $result->wid,
'description' => $result->des,
'field_image' => [
'target_id' => $fid,
'alt' => $title,
'title' => $title,
'width' => $image
->getWidth(),
'height' => $image
->getHeight(),
],
]);
try {
$photosImage
->save();
if ($photosImage) {
try {
// Move image views to the {photos_count} table.
\Drupal::database()
->insert('photos_count')
->fields([
'cid' => $photosImage
->id(),
'changed' => \Drupal::time()
->getRequestTime(),
'type' => 'image_views',
'value' => $result->count,
])
->execute();
} catch (\Exception $e) {
watchdog_exception('photos', $e);
}
}
} catch (EntityStorageException $e) {
watchdog_exception('photos', $e);
}
} catch (InvalidPluginDefinitionException $e) {
watchdog_exception('photos', $e);
} catch (PluginNotFoundException $e) {
watchdog_exception('photos', $e);
}
}
} catch (InvalidPluginDefinitionException $e) {
watchdog_exception('photos', $e);
} catch (PluginNotFoundException $e) {
watchdog_exception('photos', $e);
}
}
$sandbox['#finished'] = $sandbox['current_step'] / $sandbox['steps'];
if ($sandbox['#finished'] >= 1) {
return t('Updated to use the new photos_image entity.');
}
}
return NULL;
}