diff --git a/packages/plugin/src/Bundles/Fields/Implementations/FileUpload/FileUploadBundle.php b/packages/plugin/src/Bundles/Fields/Implementations/FileUpload/FileUploadBundle.php index e8639e850..5e067851f 100644 --- a/packages/plugin/src/Bundles/Fields/Implementations/FileUpload/FileUploadBundle.php +++ b/packages/plugin/src/Bundles/Fields/Implementations/FileUpload/FileUploadBundle.php @@ -25,7 +25,7 @@ public function __construct() Event::on(SaveForm::class, SaveForm::EVENT_SAVE_FORM, [$this, 'prolongUnfinalizedAssets']); } - public function prolongUnfinalizedAssets(SaveFormEvent $event) + public function prolongUnfinalizedAssets(SaveFormEvent $event): void { $saveTimeDays = (int) Freeform::getInstance()->settings->getSettingsModel()->saveFormTtl; $newDate = new Carbon('now +'.$saveTimeDays.' days', 'UTC'); @@ -40,7 +40,7 @@ public function prolongUnfinalizedAssets(SaveFormEvent $event) } } - public function finalizeFiles(SubmitEvent $event) + public function finalizeFiles(SubmitEvent $event): void { $form = $event->getForm(); @@ -55,7 +55,7 @@ public function finalizeFiles(SubmitEvent $event) } } - public function handleDnDPost(TransformValueEvent $event) + public function handleDnDPost(TransformValueEvent $event): void { $field = $event->getField(); if (!$field instanceof FileDragAndDropField) { diff --git a/packages/plugin/src/Fields/Interfaces/FileUploadInterface.php b/packages/plugin/src/Fields/Interfaces/FileUploadInterface.php index 85e34fbe5..1835908aa 100644 --- a/packages/plugin/src/Fields/Interfaces/FileUploadInterface.php +++ b/packages/plugin/src/Fields/Interfaces/FileUploadInterface.php @@ -12,10 +12,14 @@ namespace Solspace\Freeform\Fields\Interfaces; +use craft\elements\db\AssetQuery; + interface FileUploadInterface { public const FLAG_GLOBAL_PROPERTY = 'global-property'; + public function getAssets(): AssetQuery; + public function getAssetSourceId(): ?int; public function getDefaultUploadLocation(): ?string; diff --git a/packages/plugin/src/Integrations/Elements/User/EventListeners/PhotoTransform.php b/packages/plugin/src/Integrations/Elements/User/EventListeners/PhotoTransform.php new file mode 100644 index 000000000..66744367c --- /dev/null +++ b/packages/plugin/src/Integrations/Elements/User/EventListeners/PhotoTransform.php @@ -0,0 +1,59 @@ +getIntegration() instanceof User) { + return; + } + + if ('photo' !== $event->getHandle()) { + return; + } + + $value = $event->getValue(); + $field = $event->getFreeformField(); + + if (empty($value)) { + $event->setValue(null); + + return; + } + + if ($field instanceof FileUploadInterface) { + $asset = $field->getAssets()->one(); + if ($asset) { + $event->setValue($asset); + } + + return; + } + + $assetId = reset($value); + if ($assetId && is_numeric($assetId)) { + $asset = \Craft::$app->getAssets()->getAssetById((int) $assetId); + if ($asset) { + $event->setValue($asset); + } + } + } +}