function focal_point_update_8002 in Focal Point 8
Migrates legacy values to crop entities.
File
- ./
focal_point.install, line 31 - Install hooks for focal_point.
Code
function focal_point_update_8002(&$sandbox) {
/** @var \Drupal\file\FileUsage\FileUsageInterface $file_usage */
$file_storage = \Drupal::entityTypeManager()
->getStorage('file');
$crop_storage = \Drupal::entityTypeManager()
->getStorage('crop');
$crop_type = \Drupal::config('focal_point.settings')
->get('crop_type');
if (!isset($sandbox['num_processed'])) {
$sandbox['last_fid'] = 0;
$sandbox['num_processed'] = 0;
$sandbox['num_skipped'] = 0;
$sandbox['total_items'] = Database::getConnection()
->select('focal_point', 'fp')
->countQuery()
->execute()
->fetchField();
}
$focal_points = Database::getConnection()
->select('focal_point', 'fp')
->fields('fp')
->condition('fp.fid', $sandbox['last_fid'], '>')
->orderBy('fp.fid')
->execute()
->fetchAll();
foreach ($focal_points as $focal_point) {
/** @var \Drupal\file\FileInterface $file */
$file = $file_storage
->load($focal_point->fid);
if (!is_null($file)) {
$size = getimagesize($file
->getFileUri());
// Now we have all information we need. Let's create crop entity.
$focal_point = explode(',', $focal_point->focal_point);
$crop_storage
->create([
'type' => $crop_type,
'entity_id' => $file
->id(),
'entity_type' => 'file',
'uri' => $file
->getFileUri(),
'x' => (int) round(intval($focal_point[0]) / 100.0 * $size[0]),
'y' => (int) round(intval($focal_point[1]) / 100.0 * $size[1]),
])
->save();
$sandbox['num_processed']++;
}
else {
$sandbox['num_skipped']++;
}
$sandbox['last_fid'] = $focal_point->fid;
}
$sandbox['#finished'] = $sandbox['total_items'] ? ($sandbox['num_processed'] + $sandbox['num_skipped']) / $sandbox['total_items'] : 1;
// Intentionally leaving legacy table. You never know...
}