From 3682c88b4d5d109e13d28451fbd6d7d37191fed9 Mon Sep 17 00:00:00 2001 From: Jimmy Henderickx Date: Wed, 9 Oct 2024 13:25:22 +0200 Subject: [PATCH 1/2] Support focal point --- bynder_local.module | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/bynder_local.module b/bynder_local.module index ae3b325..ce4578a 100644 --- a/bynder_local.module +++ b/bynder_local.module @@ -59,6 +59,21 @@ function bynder_local_bynder_media_update_alter(MediaInterface $media, array $it return; } + // Update Focal point if it has changed. + if (\Drupal::moduleHandler()->moduleExists('focal_point') && isset($item["activeOriginalFocusPoint"]["x"])) { + /** @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($media->get('field_bynder_image')->entity, $crop_type); + $position = $crop->position(); + $original_relative_position = $focal_point_manager->absoluteToRelative($position['x'], $position['y'], $item["width"], $item["height"]); + $new_relative_position = $focal_point_manager->absoluteToRelative($item["activeOriginalFocusPoint"]["x"], $item["activeOriginalFocusPoint"]["y"], $item["width"], $item["height"]); + if($original_relative_position['x'] != $new_relative_position['x'] || $original_relative_position['y'] != $new_relative_position['y']) { + $focal_point_manager->saveCropEntity($new_relative_position["x"], $new_relative_position['y'], $media->get('field_bynder_image')->width, $media->get('field_bynder_image')->height, $crop); + $has_changed = TRUE; + } + } + // Check the asset date, if it changed after the local file or if there is // no local file, fetch it again. if ($media->get('field_bynder_image')->entity) { @@ -88,6 +103,17 @@ function bynder_local_bynder_media_update_alter(MediaInterface $media, array $it } $media->set('field_bynder_image', $file); + + // Store Focal point. + if (\Drupal::moduleHandler()->moduleExists('focal_point') && isset($item["activeOriginalFocusPoint"]["x"])) { + /** @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($media->get('field_bynder_image')->entity, $crop_type); + $relative_position = $focal_point_manager->absoluteToRelative($item["activeOriginalFocusPoint"]["x"], $item["activeOriginalFocusPoint"]["y"], $item["width"], $item["height"]); + $focal_point_manager->saveCropEntity($relative_position["x"], $relative_position['y'], $media->get('field_bynder_image')->width, $media->get('field_bynder_image')->height, $crop); + } + $has_changed = TRUE; } } From 0bfddd2fde7bb8352355bacd7a4f22e47cfc12a1 Mon Sep 17 00:00:00 2001 From: Jimmy Henderickx Date: Wed, 9 Oct 2024 13:28:22 +0200 Subject: [PATCH 2/2] Support focal point --- bynder_local.module | 33 +++++++++++++++++---------------- 1 file changed, 17 insertions(+), 16 deletions(-) diff --git a/bynder_local.module b/bynder_local.module index ce4578a..e628aa0 100644 --- a/bynder_local.module +++ b/bynder_local.module @@ -59,24 +59,25 @@ function bynder_local_bynder_media_update_alter(MediaInterface $media, array $it return; } - // Update Focal point if it has changed. - if (\Drupal::moduleHandler()->moduleExists('focal_point') && isset($item["activeOriginalFocusPoint"]["x"])) { - /** @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($media->get('field_bynder_image')->entity, $crop_type); - $position = $crop->position(); - $original_relative_position = $focal_point_manager->absoluteToRelative($position['x'], $position['y'], $item["width"], $item["height"]); - $new_relative_position = $focal_point_manager->absoluteToRelative($item["activeOriginalFocusPoint"]["x"], $item["activeOriginalFocusPoint"]["y"], $item["width"], $item["height"]); - if($original_relative_position['x'] != $new_relative_position['x'] || $original_relative_position['y'] != $new_relative_position['y']) { - $focal_point_manager->saveCropEntity($new_relative_position["x"], $new_relative_position['y'], $media->get('field_bynder_image')->width, $media->get('field_bynder_image')->height, $crop); - $has_changed = TRUE; + if ($media->get('field_bynder_image')->entity) { + + // Update Focal point if it has changed. + if (\Drupal::moduleHandler()->moduleExists('focal_point') && isset($item["activeOriginalFocusPoint"]["x"])) { + /** @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($media->get('field_bynder_image')->entity, $crop_type); + $position = $crop->position(); + $original_relative_position = $focal_point_manager->absoluteToRelative($position['x'], $position['y'], $item["width"], $item["height"]); + $new_relative_position = $focal_point_manager->absoluteToRelative($item["activeOriginalFocusPoint"]["x"], $item["activeOriginalFocusPoint"]["y"], $item["width"], $item["height"]); + if($original_relative_position['x'] != $new_relative_position['x'] || $original_relative_position['y'] != $new_relative_position['y']) { + $focal_point_manager->saveCropEntity($new_relative_position["x"], $new_relative_position['y'], $media->get('field_bynder_image')->width, $media->get('field_bynder_image')->height, $crop); + $has_changed = TRUE; + } } - } - // Check the asset date, if it changed after the local file or if there is - // no local file, fetch it again. - if ($media->get('field_bynder_image')->entity) { + // Check the asset date, if it changed after the local file or if there is + // no local file, fetch it again. try { $modified = new \DateTime($item['dateModified']); if ($modified->getTimestamp() < $media->get('field_bynder_image')->entity->getCreatedTime()) {