- πΊπΈUnited States firewaller
I refactored the above for D10:
/** * Implements hook_entity_presave(). */ function MY_MODULE_entity_presave(EntityInterface $entity): void { // Skip if not file. if (!$entity instanceof FileInterface) { return; } // Migrate epsa crop to focal point. $fid = $entity->id(); $migrate_database = Database::getConnection('default', 'migrate'); $query = $migrate_database->select('epsacrop_files', 'e'); $query->innerJoin('file_metadata', 'fmw', "fmw.fid = e.fid AND fmw.name = 'width'"); $query->innerJoin('file_metadata', 'fmh', "fmh.fid = e.fid AND fmh.name = 'height'"); $query->addField('e', 'coords'); $query->addField('fmw', 'value', 'width'); $query->addField('fmh', 'value', 'height'); $query->condition('e.fid', $fid); $query->range(0, 1); $results = $query->execute(); if ($results) { /** @var \Drupal\focal_point\FocalPointManagerInterface $focal_point_manager */ $focal_point_manager = \Drupal::service('focal_point.manager'); $crop_type = \Drupal::config('focal_point.settings')->get('crop_type'); $crop = $focal_point_manager->getCropEntity($entity, $crop_type); while ($record = $results->fetchAssoc()) { $coords = unserialize($record['coords']); $coords = Json::decode($coords); if (isset($coords[$fid])) { // Get first image style. $coords = reset($coords[$fid]); $focal_point_x = $coords['x'] + round($coords['w'] / 2); $focal_point_y = $coords['y'] + round($coords['h'] / 2); $width = unserialize($record['width']); $height = unserialize($record['height']); // Skip if invalid. if ( $focal_point_x > $width || $focal_point_y > $height ) { continue; } $relative = $focal_point_manager->absoluteToRelative($focal_point_x, $focal_point_y, $width, $height); $x = $relative['x']; $y = $relative['y']; $focal_point = implode(',', $relative); if ( $focal_point_manager->validateFocalPoint($focal_point) && $width && $height ) { $focal_point_manager->saveCropEntity($x, $y, $width, $height, $crop); } } } } }