diff --git a/focal_point.install b/focal_point.install index cf389eb..e85100f 100644 --- a/focal_point.install +++ b/focal_point.install @@ -67,6 +67,7 @@ function focal_point_update_8002(&$sandbox) { if (!isset($sandbox['num_processed'])) { $sandbox['last_fid'] = 0; $sandbox['num_processed'] = 0; + $sandbox['num_skipped'] = 0; $sandbox['total_items'] = \Drupal::database() ->select('focal_point', 'fp') ->countQuery() @@ -86,45 +87,52 @@ function focal_point_update_8002(&$sandbox) { /** @var \Drupal\file\FileInterface $file */ $file = $file_storage->load($focal_point->fid); - // Try to load width and height from the image fields and fall back to - // reading image file if that was not successful. - $usage = $file_usage->listUsage($file); - $usage = current($usage); + if (!is_null($file)) { + // Try to load width and height from the image fields and fall back to + // reading image file if that was not successful. + $usage = $file_usage->listUsage($file); + $usage = current($usage); - /** @var \Drupal\Core\Entity\FieldableEntityInterface $entity */ - $entity = \Drupal::entityTypeManager()->getStorage(key($usage))->load(current(current($usage))); - $size = NULL; - foreach ($entity->getFieldDefinitions() as $field_definition) { - if ($field_definition->getType() == 'image') { - foreach ($entity->{$field_definition->getName()} as $item) { - if ($item->target_id == $file->id()) { - $size = [$item->width, $item->height]; + /** @var \Drupal\Core\Entity\FieldableEntityInterface $entity */ + $entity = \Drupal::entityTypeManager() + ->getStorage(key($usage)) + ->load(current(current($usage))); + $size = NULL; + foreach ($entity->getFieldDefinitions() as $field_definition) { + if ($field_definition->getType() == 'image') { + foreach ($entity->{$field_definition->getName()} as $item) { + if ($item->target_id == $file->id()) { + $size = [$item->width, $item->height]; + } } } } - } - if (empty($size)) { - $size = getimagesize($file->getFileUri()); - } + if (empty($size)) { + $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.) * $size[0]), - 'y' => (int) round((intval($focal_point[1]) / 100.) * $size[1]), - ]) - ->save(); - $sandbox['num_processed']++; - $sandbox['last_fid'] = $file->id(); + // 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.) * $size[0]), + 'y' => (int) round((intval($focal_point[1]) / 100.) * $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['total_items'] : 1; + $sandbox['#finished'] = $sandbox['total_items'] ? ($sandbox['num_processed'] + $sandbox['num_skipped']) / $sandbox['total_items'] : 1; // Intentionally leaving legacy table. You never know... }