function photos_post_update_migrate_photos_image_entity_2 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_2()
Update {photos_album}.cover_id to use new photos_image entity id.
File
- ./
photos.post_update.php, line 113 - Post update functions for Photos module.
Code
function photos_post_update_migrate_photos_image_entity_2(&$sandbox = NULL) {
// Check if temporary table was created.
if (\Drupal::database()
->schema()
->tableExists('photos_image_tmp')) {
if (!isset($sandbox['steps'])) {
$sandbox['limit'] = 50;
$sandbox['current_step'] = 0;
$sandbox['current_id'] = 0;
// Count query.
$query = \Drupal::database()
->select('photos_album', 'a')
->fields('a', [
'cover_id',
])
->condition('a.cover_id', 0, '!=');
$num_rows = $query
->countQuery()
->execute()
->fetchField();
$sandbox['steps'] = $num_rows;
if ($sandbox['steps'] <= 1) {
// Drop the temporary table.
\Drupal::database()
->schema()
->dropTable('photos_image_tmp');
$sandbox['#finished'] = 1;
return t('No albums need to be updated.');
}
}
$results = \Drupal::database()
->select('photos_album', 'a')
->fields('a')
->condition('a.album_id', $sandbox['current_id'], '>')
->condition('a.cover_id', 0, '!=')
->orderBy('a.album_id', 'ASC')
->range(0, $sandbox['limit'])
->execute();
foreach ($results as $result) {
$sandbox['current_step']++;
$sandbox['current_id'] = $result->album_id;
$photosImageIds = \Drupal::entityQuery('photos_image')
->condition('field_image.target_id', $result->cover_id)
->execute();
$cover_id = 0;
if (!empty($photosImageIds)) {
$cover_id = reset($photosImageIds);
}
\Drupal::database()
->update('photos_album')
->fields([
'cover_id' => $cover_id,
])
->condition('album_id', $result->album_id)
->execute();
}
$sandbox['#finished'] = $sandbox['current_step'] / $sandbox['steps'];
if ($sandbox['#finished'] >= 1) {
// Drop the temporary table.
\Drupal::database()
->schema()
->dropTable('photos_image_tmp');
return t('Updated album cover id to use photos_image entity id.');
}
}
return NULL;
}