- Issue created by @datawench
- πΊπΈUnited States datawench
Scratch that. I figured out how to use your event system to avoid hacking. I'm closing this, and showing my code here in case anyone's interested.
namespace Drupal\my_custom\EventSubscriber; use Drupal; use Drupal\file\Entity\File; use Drupal\focal_point\FocalPointManager; use Drupal\media\Entity\Media; use Drupal\focal_point\FocalPointManagerInterface; use Drupal\single_content_sync\Event\ExportEvent; use Symfony\Component\EventDispatcher\EventSubscriberInterface; class ExportEventSubscriber implements EventSubscriberInterface { private array $imgMimeTypes = [ 'image/jpeg', 'image/gif', 'image/png', 'image/webp', ]; private FocalPointManagerInterface $focalPointManager; private string $cropType; public function __construct() { $this->focalPointManager = new FocalPointManager(Drupal::service('entity_type.manager')); $this->cropType = Drupal::config('focal_point.settings') ->get('crop_type'); } /** * @return array[] */ public static function getSubscribedEvents(): array { return [ ExportEvent::class => ['onExport'], ]; } /** * @param \Drupal\single_content_sync\Event\ExportEvent $event * * @return void */ public function onExport(ExportEvent $event) { if ('file' == $event->getEntity()->getEntityTypeId()) { /** @var File $file */ $file = $event->getEntity(); $content = $event->getContent(); if (in_array($file->getMimeType(), $this->imgMimeTypes)) { $crop = $this->handleFocalPoint($file); if (!empty($crop)) { $content['base_fields']['crop'] = $crop; $event->setContent($content); } } } } /** * Extract the actual focal point dimensions. * * @param \Drupal\file\Entity\File $file * * @return array */ private function handleFocalPoint(File $file): array { /** @var \Drupal\crop\Entity\Crop $cropEntity */ $cropEntity = $this->focalPointManager->getCropEntity($file, $this->cropType); $fid = $file->id(); if (!$cropEntity->isNew() && !$cropEntity->get('x') ->isEmpty() && !$cropEntity->get('y') ->isEmpty()) { $media_ids = Drupal::entityQuery('media') ->accessCheck(FALSE) ->condition('field_media_image.target_id', $fid) ->execute(); // to populate the crop height and width, // we need to back-reference to the media entity that uses this image, // because the crop entity isn't saving it. if (!empty($media_ids)) { $media_id = reset($media_ids); $media = Media::load($media_id); $height = !empty($media->get('field_original_image_height') ->getValue()) ? $media->get('field_original_image_height') ->getValue()[0]['value'] : 0; $width = !empty($media->get('field_original_image_width') ->getValue()) ? $media->get('field_original_image_width') ->getValue()[0]['value'] : 0; $x = !empty($cropEntity->get('x') ->getValue()) ? $cropEntity->get('x') ->getValue()[0]['value'] : 0; $y = !empty($cropEntity->get('y') ->getValue()) ? $cropEntity->get('y') ->getValue()[0]['value'] : 0; $cropDims = $this->focalPointManager->absoluteToRelative($x, $y, $width, $height); $cropDims['height'] = $height; $cropDims['width'] = $width; return $cropDims; } return []; } return []; } }